Opened 14 years ago

Last modified 2 years ago

#12238 new

ModelAdmin ignores dynamic fields of ModelForm — at Version 3

Reported by: anonymous Owned by: nobody
Component: contrib.admin Version: 1.1
Severity: Normal Keywords: modelform modeladmin dynamic field
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ramiro Morales)

If a ModelForm is created and then modified to programatically add fields (say, in __init__), ModelAdmin ignores these fields when rendering the form. If one of these fields is added to the ModelForm's Meta, the field shows up just fine.

I would expect the field to display without the coaxing in Meta.fields.

  1. Create a ModelForm
  2. Add it to ModelAdmin
  3. View Form
  4. Update ModelForm's __init__ to include self.fields['xyz'] = forms.CharField(max_length=255, initial='keke')
  5. View Form (note no change)
  6. Update ModelForm's Meta.fields to include "xyz"
  7. View Form (note the change)

Change History (3)

comment:1 by anonymous, 14 years ago

Component: Uncategorizeddjango.contrib.admin

I've solved this in the meantime by updating contrib/admin/options.py in:

    def get_fieldsets(self, request, obj=None):
        "Hook for specifying fieldsets for the add form."
        if self.declared_fieldsets:
            return self.declared_fieldsets
        #form = self.get_form(request, obj)
        #return [(None, {'fields': form.base_fields.keys()})]
        form = self.get_form(request, obj)(instance=obj)
        return [(None, {'fields': form.fields.keys()})]

Seems to be a harmless fix, possible to get this applied upstream?

comment:2 by anonymous, 14 years ago

Has patch: set

comment:3 by Ramiro Morales, 14 years ago

Description: modified (diff)
Has patch: unset

(reformat description, also it hasn't a patch until it has a patch)

Note: See TracTickets for help on using tickets.
Back to Top