Saturday, March 30, 2019

Re: ModelForm for model that includes multiple items of one kind

Hi,

To connect multiple forms you should use: FormSets 


Or



Read about it in the above links.

Regards,
Kiran Capoor

On Sunday, March 31, 2019 at 3:56:58 AM UTC+5:30, Dean Karres wrote:
Hi

I am looking for some advice.  I have a basic Profile model.  It looks something like:

Profile:
first_name
middle_name
last_name
birthday
address_line1
address_line2
city
state
zip
email
phone

I have a ModelForm that goes with this.

This is sufficient for nearly all the cases I am trying to deal with.  However, there are a few special cases in which a person needs to list two or more addresses; two or more email addrs; two or more phone numbers; or, combinations.  

I know just enough about database normalization to think that I need the Profile model plus Address, Email and Phone models and have the Profile have a ForeignKey relation to each of the others.  A la:

Profile:
first_name
middle_name
last_name
birthday
<address-foreign-key>
<email-foreign-key>
<phone-foreign-key>

Address
address_line1
address_line2
City
state
zip

Email
email

Phone
phone

My question is more about the associated ModelForm(s).  First, I currently have a ModelForm for the first Profile model without the foreign keys.  If I break out the address, email and phone portions into separate models do I need to create separate model forms for each new model?  If so would the new Profile ModelFOrm just inherit the ModelForms of the other components. Something like:

class ProfileForm(ModelForm, AddressForm, EmailForm, PhoneForm):
...

The current ModelForm has "clean_<field>" "clean" methods.  Do I keep that in the new Profile form?  Do I put the various "clean" methods in their own model forms and hope that inheritance will pull them in?

Next, how does a ModelForm deal with more than one of the same item -- like more then one address?

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/577bc4d2-4ac9-4034-9d82-489bdd013c9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment