On Sun, 2009-03-22 at 05:39 -0700, nivhab wrote:
> Hi,
> 
> I have the following model:
> 
>    class product(models.Model):
>       productId       = models.AutoField(primary_key=True)
>       name            = models.CharField(max_length=200, unique=True)
>       description  = models.CharField(max_length=200, blank=True)
> 
>   class Wishlist(models.Model):
>       wishId         = models.AutoField(primary_key=True)
>       product       = models.ForeignKey(Product)
>       for_user      = models.ForeignKey(User)
>       notes          = models.CharField(max_length=200, blank=True)
> 
> I would like to create one form that lets the user (unknowingly) to
> create new product and a wish list related to this product in one
> request. The form needs to contain fields for both models.
> 
> I have some very ugly method for doing it but there has to be a
> cleaner way. I simply couldn't find any in the docs or examples.

You're probably over-thinking this.

You want a form with some fields in it. When the form is submitted, your
view takes the cleaned data from the form, uses that to determine the
appropriates values to use to create each model instance and then saves
the models.

So create a normal Form subclass (that's documented in the forms
documentation), then use the cleaned data (forms documentation) to
create model instances (model documentation or the tutorial) and save
those.

When you forms don't map directly onto models, then that's fine. That's
why the form module is separate from the model module in Django, so that
you're not required to only create forms based on models.

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