Friday, October 16, 2009

Building a form to write one of two models

Good morning,
I have two models that have mostly-similar fields (they both inherit
from a single, abstract model in Django) and one different field. I'd
really like it to be two separate tables in the database for
normalization (each has a separate, one-to-many relationship with
another table and the two are of distinct type and do not intersect).

However, I would like to make a form that will seamlessly create (or
edit) a row in one of these two tables. So, at first I set out to make
a generic form (extending django.forms.Form) that displayed the
fields, as well as a "type" ChoiceField that determined whether to
save an instance of the first model or the second. Then, for editing,
I simply have straight, unrelated django.forms.ModelForm classes that
are instantiated.

My problem: That's not DRY, and I don't feel like it's the right way
to do it. So, I'm wondering if anyone can fill me in on a better way
to...
- if the form is unbound, present the form, plus a "type" dropdown to
assess where to place the information on save (I also need one
dependent field, but I can do that with simple JavaScript and an
intelligent save() method).
- if the form is bound, do not include the "type" field (as I don't
want the user to be able to change it)

I'm pretty sure my solution is off base. Can someone enlighten me
regarding a better way? Much obliged!

Thanks,
Luke Sneeringer

--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate