Sunday, August 5, 2012

dynamic fildsets on ModelAdmin

Hi, I've been struggling with dynamic forms and until now I'm loosing the battle :S
The thing is that I have a HomeAdmin(ModelAdmin) that uses the form
HomeAdminForm(ModelForm)

The HomeAdminForm redefines the __init__ method to add a bunch a of hidden
input fields like this :

class HomeAdminForm(forms.ModelForm):
   
    def __init__(self, *args, **kwargs):
            super(HomeAdminForm, self).__init__(*args, **kwargs)
            #add all the attribute types
            for at in HomeAttrType.objects.all():
                self.fields['attr_type_%s' % at.name] = forms.CharField(
                     widget=forms.HiddenInput(attrs={'class': 'hidden'}), label='', bound=True)


So, now I want the HomeAdmin to render all this stuff into the from, but when I try to
add this fields into the fieldset attribute and load the page it fails saying.
Unknown field(s) (attr_type_foo, attr_type_prueba, attr_type_carnasa) specified for Home


Those fields are the ones I added at HomeAdminForm.__init__ this way

class HomeAdmin(admin.ModelAdmin):
   
    form = HomeAdminForm

    def __init__(self, *args, **kwargs):
        super(HomeAdmin, self).__init__(*args, **kwargs)
        hidden_fields = []
        for at in HomeAttrType.objects.all():
            hidden_fields.append('attr_type_%s' % at.name)
        self.fieldsets.append((None, {'fields' : hidden_fields, }))

The problem seems to be that the constructor for HomeAdminForm gets called after
the constructor for HomeAdmin, so all the added fields are not there yet when
HomeAdmin.__init__ is called.

Is what I'm trying do ok ?,  Is there any way to fix this ?

Thanks dudes,

--
Nicolas Emiliani

Lo unico instantaneo en la vida es el cafe, y es bien feo.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate