On Thu, 2007-03-08 at 21:04 -0600, Greg Donald wrote:
> How can I add a field in a join table?
> 
> I have this:
> 
> class Item(models.Model):
>     name = models.CharField(maxlength=32)
> 
> class List(models.Model):
>     name = models.CharField(maxlength=32)
>     items = models.ManyToManyField(Item)
> 
> which creates the 'list_items' join table fine, but then how do I put
> a field in that same table?
> 
> When I try to add something like this:
> 
> class List_Items(models.Model):
>     quantity = models.IntegerField()
> 
> I get errors:
> 
> _mysql_exceptions.OperationalError: (1050, "Table 'stuff_list_items'
> already exists")
> 
> Does Django have join models or the like?

You cannot do this in Django. ManyToManyField is a shortcut for a direct
relation with no annotation fields like this.

If you want to use the intermediate table, you will need to create it
and use two ForeignKeys (to each end of the relation). At the database
level it works out exactly the same. At the Python level, your queries
will sometimes have an extra field in them because you have to manually
walk through the join table.

See http://www.djangoproject.com/documentation/models/m2m_intermediary/
for an example.

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 [email protected]
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