Blog » Blog Entry

Django's missing join model

March 9, 2007

Django has no sense of a join model. Sure, you get a join table but it's unaware of connecting models. The specific issue this missing features causes me is that I can't (automatically) add an extra field in the join table Django creates for me. For example, this is illegal:

class Item(models.Model):
   name = models.CharField(maxlength=32)
class List(models.Model):
   name = models.CharField(maxlength=32)
   items = models.ManyToManyField(Item)
class List_Items(models.Model):
   quantity = models.IntegerField()

I get these errors:

_mysql_exceptions.OperationalError: (1050, "Table
'stuff_list_items' already exists")

Instead I am having to settle for something lesser, like this:

class Item(models.Model):
    name = models.CharField(maxlength=32)
class List(models.Model):
    name = models.CharField(maxlength=32)
class List_Item(models.Model):
    item = models.ForeignKey(Item)
    list = models.ForeignKey(List)
    quantity = models.IntegerField()

The Item model doesn't know about the List model and vise-versa. And some of the stuff in the free Django admin doesn't work and will have to be hand-coded, oh the horror!

I'd like to point out that Rubyonrails has join tables, and even extends the behavior into other associated models using :through

many-to-many-dance-off

I could easily do list.items.first.list and such in Rails.

Tags: ruby, rubyonrails, python, django

« Dear Microsoft, IE7 sucks Django Apache vhost »

Add a comment:

Title:

Comment:

Name:

Email: