Django

Code

Ticket #12238 (new)

Opened 10 months ago

Last modified 7 months ago

ModelAdmin ignores dynamic fields of ModelForm

Reported by: anonymous Assigned to: nobody
Milestone: Component: django.contrib.admin
Version: 1.1 Keywords: modelform modeladmin dynamic field
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description (Last modified by ramiro)

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)

Attachments

dynamic-fieldsets-0.1.diff (1.9 kB) - added by batiste on 01/20/10 07:57:50.
Attempt to use dynamic fieldsets generation

Change History

11/18/09 20:51:58 changed by anonymous

  • needs_better_patch changed.
  • component changed from Uncategorized to django.contrib.admin.
  • needs_tests changed.
  • needs_docs changed.

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?

11/18/09 21:21:35 changed by anonymous

  • has_patch set to 1.

11/19/09 03:31:21 changed by ramiro

  • has_patch deleted.
  • description changed.

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

01/20/10 05:52:48 changed by batiste

I think the declared_fieldsets too satic too. Why not using a callable that people can override?

I can provide a patch if needed.

01/20/10 07:57:50 changed by batiste

  • attachment dynamic-fieldsets-0.1.diff added.

Attempt to use dynamic fieldsets generation

01/20/10 08:18:43 changed by batiste

  • has_patch set to 1.

The patch I sent seems to work quite oki, but if I add a field dynamicaly, even if it's in the form object, the new value is not saved:

    def dynamic_fieldsets(self, request, obj=None, **kwargs):

        general_fields = list(self.general_fields)
        perms = PagePermission(request.user)

        if perms.check('freeze'):
            general_fields.append('freeze_date')

        return [
            [_('General'), {
                'fields': general_fields,
                'classes': ('module-general',),
            }],
            (_('Options'), {
                'fields': self.normal_fields,
                'classes': ('module-options',),
            }),
        ]

My freeze field is added properly in the admin interface, but the value is not saved. But in the other hand if I do things like that:

    def get_fieldsets(self, request, obj=None):
        """
        Add fieldsets of placeholders to the list of already
        existing fieldsets.
        """
        general_fields = list(self.general_fields)
        perms = PagePermission(request.user)

        # some ugly business to remove freeze_date
        # from the field list
        general_module = {
            'fields': list(self.general_fields),
            'classes': ('module-general',),
        }

        default_fieldsets = list(self.fieldsets)
        if not perms.check('freeze'):
            general_module['fields'].remove('freeze_date')

        default_fieldsets[0][1] = general_module

The latest version works, but it feels like a very hackish way to do things.

Can somebody explain me what's wrong here? Does Django have a more elegant way to do this?

02/05/10 10:18:44 changed by russellm

  • needs_tests set to 1.
  • stage changed from Unreviewed to Accepted.

Add/Change #12238 (ModelAdmin ignores dynamic fields of ModelForm)




Change Properties
Action