Thanks for the help :) Seems like a very sound approach, not least
because I understand it!

--
James

On Oct 4, 3:53 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Thu, 2007-10-04 at 07:37 -0700, James Mulholland wrote:
> > Hi,
>
> > [Note: apologies for the double post. Google didn't offer to preview
> > the message, which is what I was trying to do...]
>
> > I'm starting to investigate newforms in detail (having previously used
> > the old forms library) since I have a job to convert an Access DB to
> > web format. The client wants to maintain a similar layout to what they
> > have in Access, which in places means having one page which has
> > several records on it. To test out some ideas, I've made a simple app
> > which attempts to edit two database records from the same web page.
>
> > models.py
>
> > class Country(models.Model):
> >     name = models.CharField(maxlength=100)
> >     iso  = models.CharField(maxlength=2)
>
> > class Address(models.Model):
> >     address_1 = models.CharField(maxlength=60)
> >     address_2 = models.CharField(maxlength=60, null=True, blank=True)
> >     town      = models.CharField(maxlength=30)
> >     county    = models.CharField(maxlength=40)
> >     country   = models.ForeignKey(Country)
>
> > views.py
>
> > def edit_address(request, id=None):
> >     address = Address.objects.get(pk=id)
> >     country = address.country
>
> >     AddressForm = form_for_instance(address, fields=('address_1',
> > 'address_2', 'town', 'county'))
> >     CountryForm = form_for_instance(country, fields=('iso', 'name'))
>
> >     class FullForm(forms.Form, AddressForm, CountryForm):
> >         pass
>
> This is the problem line. You are hoping for behaviour from multiple
> inheritance works in Python that isn't valid. Firstly, there would be
> some problems here because the save() method will only call the save()
> method on one of the subclasses, not all of them. Probably other
> unintended side-effects as well.
>
> Instead of trying to create a mega-form like this, just work with the
> two forms independently. You can initialise a form using a data
> dictionary (request.POST in this case) that contains more information
> than is required for the form. The form class will only use the fields
> it requires.
>
> So after creating AddressForm and CountryForm, the continue with:
>
>         address_form = AddressForm(request.POST)
>         country_from = CountryForm(request.POST)
>
> and so forth. You can happily pass both forms to the template for
> rendering and everything like that.
>
> Alternatively, if you truly do want only a single form class, you can
> write your own variation on form_for_instance that takes multiple
> instances from different classes and does whatever you expect. I would
> expect that just using two form classes will generally lead to more
> readable code, though, unless you are doing this a lot with lots of
> classes on a page (in which case I'd write the helper function).
>
> Regards,
> Malcolm


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to