views:

885

answers:

3

I'm working on a Web service in Django, and I need to model a very specific, complex relationship which I just can't be able to solve.

Imagine three general models, let's call them Site, Category and Item. Each Site contains one or several Categories, but it can relate to them in one of two possible ways: one are "common" categories, which are in a many-to-many relationship: they are predefined, and each Site can relate to zero or more of the Categories, and vice versa. The other type of categories are individually defined for each site, and one such category "belongs" only to that site and none other; i.e. they are in a many-to-one relationship, as each Site may have a number of those Categories.

Internally, those two type of Categories are completely identical, they only differ in the way they are related to the Sites. It could, however, separate them in two different models (with a common parent model probably), but that solves only half of my problem: the Item model is in a many-to-one relationship with the Categories, i.e. each Item belongs to only one Category, and ideally it shouldn't care how it is related to a Site.

Another solution would be to allow the two separate types of Site-Category relations to coexist (i.e. to have both a ForeignKey and a ManyToMany field on the same Category model), but this solution feels like opening a whole other can of worms.

Does anyone have an idea if there is a third, better solution to this dead end?

+4  A: 

Why not just have both types of category in one model, so you just have 3 models?

Site

Category
  Sites = models.ManyToManyField(Site)
  IsCommon =   models.BooleanField()

Item
  Category = models.ForeignKey(Category)

You say "Internally, those two type of Categories are completely identical". So in sounds like this is possible. Note it is perfectly valid for a ManyToManyField to have only one value, so you don't need "ForeignKey and a ManyToMany field on the same Category model" which just sounds like a hassle. Just put only one value in the ManyToMany field

James
Thanks to all for the replies. I have decided to go this route, with an added bonus it allowed: each of the common categories may belong to a pre-defined group, so now instead of the IsCommon flag I can have a group ForeignKey, and if it is NULL the category is single site and not common.Thanks again!
Berislav Lopac
A: 

Caveat: I know Object-Relation mapping, Rails, and Python, but not Django specifically.

I see two additinal options:

  1. Thinking from a database point of view, I could make the table needed for the many-many relation hold an additional field which indicates a "common" vs. "site" relationship and add constraints to limit the type of "site" relationships. This can be done in Django, I think, in the section "Extra Fields on Many-To-Many Relationships."

If you are at an earlier version of Django, you can still do this by making the many-many-table an explict model.

  1. Thinking from an object point of view, I could see splitting the Categories into three classes:

    BaseCategory

    CommonCategory(BaseCategory)

    SiteCategory(BaseCategory)

and then use one of Django's inheritance models.

Kathy Van Stone
+1  A: 

As as alternative implementation you could use django content types (generic relations) to accomplish the connection of the items. A bonus for using this implementation is that it allows you to utilize the category models in different ways depending on your data needs down the road.

You can make using the site categories easier by writing model methods for pulling and sorting categories. Django's contrib admin also supports the generic relation inlines.

Your models would be as follow:

Site(models.Model):
  label = models.CharField(max_length=255)

Category(models.Model):
  site = models.ManyToManyField(Site)
  label = models.CharField(max_length=255)

SiteCategory(models.Model):
  site = models.ForeignKey(Site)
  label = models.CharField(max_length=255)

Item(models.Model):
  label = models.CharField(max_length=255)
  content_type = models.ForeignKey(ContentType)
  object_id = models.PositiveIntegerField()
  content_object = generic.GenericForeignKey('content_type', 'object_id')

For a more in depth review of content types and how to query the generic relations you can read here: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/

wlashell