Re: dynamic fildsets on ModelAdmin

2012-08-05 Thread Nicolas Emiliani
On Sun, Aug 5, 2012 at 5:40 PM, Melvyn Sopacua wrote:

> On 5-8-2012 20:16, Nicolas Emiliani wrote:
> > Hi, I've been struggling with dynamic forms and until now I'm loosing the
> > battle :S
>
> > 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.
>
> Sorry to say this, but you have to go back to the drawing board. You
> cannot add random fields to fieldset. They *have* to be models.Field
> classes of the model attached the ModelAdmin so that the values get
> cleaned properly.
> If you want extra fields for the form, you do this via inlines for
> related objects or you subclass a ModelForm and set the form attribute
> of the ModelAdmin.
>
>
Ups. Well, thanks anyways. Since my drawing board is full of drawings,
and they are not happy faces, I'll create another post with what I want
to accomplish and maybe you can suggest something.



> --
> Melvyn Sopacua
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>


-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: dynamic fildsets on ModelAdmin

2012-08-05 Thread Melvyn Sopacua
On 5-8-2012 20:16, Nicolas Emiliani wrote:
> Hi, I've been struggling with dynamic forms and until now I'm loosing the
> battle :S

> 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.

Sorry to say this, but you have to go back to the drawing board. You
cannot add random fields to fieldset. They *have* to be models.Field
classes of the model attached the ModelAdmin so that the values get
cleaned properly.
If you want extra fields for the form, you do this via inlines for
related objects or you subclass a ModelForm and set the form attribute
of the ModelAdmin.

-- 
Melvyn Sopacua

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



dynamic fildsets on ModelAdmin

2012-08-05 Thread Nicolas Emiliani
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.