"Why does the following not give a final model form with 3 fields?
The two extra fields are not available. If I move them directly into the
model form, it works but I wanted to declare these fields in a separate
form because I plan on reusing them in several forms.
Is there a way to do that?"

I also meant to answer this: The parent form with the 'extra' fields needs
to inherit from forms.ModelForm, not just forms.Form, otherwise the child
ModelForm is unhappy. You would also need to update the fields value in
Meta to include the fields from the parent form, but my example shows that.

Also, I didn't test this, but you may need to declare a save() function in
the parent form, otherwise your two fields will not be saved because Django
has no idea what to do with them. If these extra fields are going to be
saved using the same model as the rest of the model form, you would be
better off with inheriting from abstract models and leaving the modelform
alone.

-James

On Sat, Nov 15, 2014 at 12:25 AM, James Schneider <jrschneide...@gmail.com>
wrote:

> I think you're overcomplicating things. Check this out:
>
>
> https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#form-inheritance
> http://chriskief.com/2013/06/30/django-form-inheritance/
>
> Try this:
>
> class ModelFormExtraFields(forms.ModelForm):
>     extra_field_1 = forms.CharField(required=False)
>     extra_field_2 = forms.CharField(required=False)
>
>     class Meta:
>         fields = ('extra_field_1', 'extra_field_2')
>
> class ModelFormA(ModelFormExtraFields):
>     email = forms.CharField(required=False)
>     blah = forms.CharField(required=False)
>
>     class Meta(ModelFormExtraFields.Meta):
>         model = ModelA
>         fields = ModelFormExtraFields.Meta.fields + ('email', 'blah')
>         # or as the last fields
>         #fields = ('email', 'blah') + ModelFormExtraFields.Meta.fields
>
> I don't even really think the Meta class in ModelFormA even needs to
> inherit directly, so just having a straight Meta: class may save some
> keystrokes, but I didn't test that.
>
> -James
>
>
> On Fri, Nov 14, 2014 at 6:15 AM, Michael Palumbo <
> michael.palumb...@gmail.com> wrote:
>
>> Thanks for your response James.
>> I am not well familiar with the metaclasses, I do not really see how to
>> do it.
>>
>> What do you think of adding the fields in the init this way?
>>
>> extra_fields = {'extra1': forms.CharField(required=False), 'extra2':
>> forms.CharField(required=False)}
>>
>> class ModelFormA(forms.ModelForm):
>>     def __init__(self, *args, **kwargs):
>>         super(ModelFormA, self).__init__(*args, **kwargs)
>>         for name, field in extra_fields.iteritems():
>>             self.fields[name] = field
>>
>> Thanks
>>
>>
>> On Thu, Nov 13, 2014 at 11:38 PM, James Schneider <
>> jrschneide...@gmail.com> wrote:
>>
>>> Modify your Meta class to include the extra fields on the model form.
>>> The Meta classes are not inherited.
>>>
>>> You may also need to override the save() method on your model form to
>>> actually save the contents of those extra fields in the inherited form.
>>>
>>> -James
>>> On Nov 13, 2014 6:32 PM, "Michael" <michael.palumb...@gmail.com> wrote:
>>>
>>>> Hi,
>>>>
>>>> Why does the following not give a final model form with 3 fields?
>>>> The two extra fields are not available. If I move them directly into
>>>> the model form, it works but I wanted to declare these fields in a separate
>>>> form because I plan on reusing them in several forms.
>>>> Is there a way to do that?
>>>>
>>>> class FormA(forms.Form):
>>>>     extra_field_1 = forms.CharField(required=False)
>>>>     extra_field_2 = forms.CharField(required=False)
>>>>
>>>> class ModelFormA(FormA, forms.ModelForm):
>>>>     class Meta:
>>>>         model = ModelA
>>>>         fields = ['email']
>>>>
>>>> Thanks
>>>> Mike
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to django-users+unsubscr...@googlegroups.com.
>>>> To post to this group, send email to django-users@googlegroups.com.
>>>> Visit this group at http://groups.google.com/group/django-users.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/django-users/53753989-9f3b-4a46-b96a-5edf7d7ccd69%40googlegroups.com
>>>> <https://groups.google.com/d/msgid/django-users/53753989-9f3b-4a46-b96a-5edf7d7ccd69%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>  --
>>> You received this message because you are subscribed to a topic in the
>>> Google Groups "Django users" group.
>>> To unsubscribe from this topic, visit
>>> https://groups.google.com/d/topic/django-users/cwTbH2F7kZM/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to
>>> django-users+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CA%2Be%2BciW0NwTDLRKZH9D5nPOCQfbqpen6ng_EH24B6bJ6d-xdUA%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-users/CA%2Be%2BciW0NwTDLRKZH9D5nPOCQfbqpen6ng_EH24B6bJ6d-xdUA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CALa-rBzc17qzBHBB3m6MkVR0nv3FchjpODyd_KAsQnTju9S5%3Dg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CALa-rBzc17qzBHBB3m6MkVR0nv3FchjpODyd_KAsQnTju9S5%3Dg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXq0PfTXdeCb_1pijrERuTivcKD%3DP-uC9-qmEUGxoRazw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to