Blog » Blog Entry

Django's missing join model

Updated: December 31, 2007March 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 Migration to Lighttpd »

Has this been fixed?

By: Sumit Chachra <sumituee at hotmail dot com>

Posted: 1 year ago

Hi,

Has this issue been fixed or looked into? Does Django 1.0 offer any relief?

Fixed!

By: Jacob Kaplan-Moss <jacob at jacobian dot org>

Posted: 1 year ago

Just as an FYI: Django 1.0 contains support for intermediary many-to-many models. See http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships.

Solved

By: Sumit Chachra <sumituee at hotmail dot com>

Posted: 1 year ago

Solved: http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

Add a comment:

Title:

Comment:

Name:

Email:

Website: