On Sun, 2008-12-21 at 13:18 -0800, stormlifter wrote:
> Well that kind of dodges the question.
> I two am looking for a way to mix and match a ModelForm

No, it doesn't dodge the question (although, because you top-posted, the
response was hidden: the suggestion was to use two form objects).
ModelForms are designed to be a simple form wrapper around a simple
model. Forms (including ModelForms) are designed to represent part of an
HTML form (not the entire form) so that you can deliberately use more
than one Django Form object in a single HTML form.

Thus, if you want to use two ModelForms in the same form, you can. If
you want to use fields from multiple models in a specific order on the
page (intermingled), then you should construct the form manually (as a
Form subclass) and your view will be responsible for extracting the
right fields and saving them to the right model.

You are trying to do something that is beyond the scope of ModelForm, in
other words.

Regards,
Malcolm


> I've got...
> 
> # Create your models here.
> class PersonForm(ModelForm):
>     class Meta:
>         model = Person
>         exclude = ('address',)
> 
> class AddressForm(ModelForm):
>     class Meta:
>         model = Address
> 
> 
> ============================ models.py ==============
> 
> class Person(models.Model):
>         first_name = models.CharField(max_length=30)
>         last_name = models.CharField(max_length=30)
>         status = models.CharField(max_length=2,
> choices=STATUS_CHOICES)
>         gender = models.CharField(max_length=1,
> choices=GENDER_CHOICES)
>         address = models.ForeignKey('Address')
> 
>          # definitions
>         def __unicode__(self):
>                 return '%s %s' % (self.first_name, self.last_name)
> 
>         def full_name(self):
>                 return '%s %s' % (self.first_name, self.last_name)
> 
>         def get_absolute_url(self):
>                 return "/person/%i/" % self.id
>         #================================================
> 
> 
> 
> class Address(models.Model):
>         line_1 = models.CharField(max_length=60)
> #     line_2 = models.CharField(max_length=60,blank=True,null=True)
>         city = models.CharField(max_length=30)
>         state = USStateField()
>         zip_code = models.CharField(max_length=10)
> On Nov 4, 5:28 am, "Jonathan Buchanan" <jonathan.bucha...@gmail.com>
> wrote:
> > On Tue, Nov 4, 2008 at 9:59 AM, Håkan Waara <hwa...@gmail.com> wrote:
> >
> > > I would love any ideas or feedback around this area, and to hear of
> > > how others in the community do it.
> >
> > > /Håkan
> >
> > I would just use two Forms.
> >
> > Forms:
> >
> >     class EditUserForm(forms.ModelForm):
> >         class Meta:
> >             model = User
> >             fields = (email, first_name, last_name)
> >
> >     class EditProfileForm(forms.ModelForm):
> >         class Meta:
> >             model = Profile
> >             fields = (avatar, location)
> >
> > View:
> >
> >     def edit_user(request, user_id):
> >         user = get_object_or_404(User, id=user_id)
> >         profile = user.get_profile()
> >
> >         if request.method == 'POST':
> >             user_form = EditUserForm(request.POST, instance=user)
> >             profile_form = EditProfileForm(request.POST,
> >                                            instance=profile)
> >             if all([user_form.is_valid(), profile_form.is_valid()]):
> >                 user_form.save()
> >                 profile_form.save()
> >                 return HttpResponseRedirect(
> >                     reverse('user_profile', args=[user_id]))
> >         else:
> >             user_form = EditUserForm(instance=user)
> >             profile_form = EditProfileForm(instance=profile)
> >
> >         return render_to_response('edit_user.html', {
> >             'user_': user,
> >             'user_form': user_form,
> >             'profile_form': profile_form,
> >         }, context_instance=RequestContext(request))
> >
> > Template:
> >
> >     <form action="{% url edit_user user_.id %}" method="POST">
> >       {{ user_form.as_p }}
> >       {{ profile_form.as_p }}
> >       <div class="buttons">
> >         <input type="submit" value="Edit">
> >         <a href="{% url user_profile user_.id %}">Cancel</a>
> >       </div>
> >     </form>
> >
> > Regards,
> > Jonathan.
> > 
> 


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