pythondjangofieldformsetinline-formset

How to add custom fields to InlineFormsets?


I'm trying to add custom fields to an InlineFormset using the following code, but the fields won't show up in the Django Admin. Is the InlineFormset too locked down to allow this? My print "ding" test fires as expected, I can print out the form.fields and see them all there, but the actual fields are never rendered in the admin.

admin.py

from django.contrib import admin
import models
from django.forms.models import BaseInlineFormSet
from django import forms
from forms import ProgressForm
from django.template.defaultfilters import slugify

class ProgressInlineFormset(BaseInlineFormSet):
    def add_fields(self, form, index):
        print "ding"
        super(ProgressInlineFormset, self).add_fields(form, index)
        for criterion in models.Criterion.objects.all():
            form.fields[slugify(criterion.name)] = forms.IntegerField(label=criterion.name)

class ProgressInline(admin.TabularInline):
    model = models.Progress
    extra = 8
    formset = ProgressInlineFormset

class ReportAdmin(admin.ModelAdmin):
    list_display = ("name", "pdf_column",)
    search_fields = ["name",]
    inlines = (ProgressInline,)

admin.site.register(models.Report, ReportAdmin)

Solution

  • model = models.Progress
    

    In the admin there will be only the fields defined in this Progress model. You have no fields/fieldsets option overwriting it.

    If you want to add the new ones, there are two options:

    Take a look at fields, fieldsets.