Finally , I got it.

class ItemForm(ModelForm):
    item  = forms.CharField(label='Item')
    class Meta:
        model=Item

    def __init__(self, *args, **kwargs):
        super(ItemForm, self).__init__(*args, **kwargs)
        if kwargs.has_key('instance'):
            del self.fields['item']
            del self.base_fields['item']
        else:
            self.fields['item'] = forms.CharField(label='Item')
            self.base_fields['item'] = forms.CharField(label='Item')


On 1 fev, 19:13, andmart <andm...@gmail.com> wrote:
> There is an error in code above.
>
> I forgot to write to message the call to super.
>
> Here is the code I'm  trying to work
>
> http://dpaste.com/375663/
>
> Thanks in advance for any help
>
> On 1 fev, 17:48, andmart <andm...@gmail.com> wrote:
>
> > Hi all,
>
> > I'm trying to remove or add fields based in presence of 'instance'
> > parameter in ModelForm __init__ like this:
>
> > class ItemForm(ModelForm):
> >        class Meta:
> >               model = Item
>
> >         def __init__(self, *args, **kwargs):
> >                if not kwargs.has_key('instance'):
> >                       self.fields['extra_field'] =
> > forms.CharField(label='Extra Field')
>
> > But debugging, in django/contrib/admin/options.py, in add_view method,
> > django instantiates my modelform and after that forget it.
>
> > I saw many posts googling with solution above - adding fields in
> > self.fields. Am I doing something wrong or is it not possible in
> > django 1.2.3 version?
>
> > Thanks in advance.

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

Reply via email to