mehmet
mehmet

Reputation: 8154

Whether to (and how to) handle multiple many-to-many relationship through a common intermediary/join table in django?

I have several models that implement a so called 'Taggable' behavior:

class Tag(models.Model):
    name = models.CharField(max_length=200)


class Taggable(models.Model):
    tags = models.ManyToManyField(Tag)

    class Meta:
        abstract = True


class A(Taggable):
...

class B(Taggable):
...

class C(Taggable): 
...

class D(Taggable):
...

This scenario causes an intermediary table to be created for each and every model inheriting from Taggable. I.e.,

appname_a_tags
appname_b_tags
appname_c_tags
appname_d_tags

I am still at the beginning of the development, and the number of such models and thus tables might increase. So, it bothers me a little to have a clutter of tables with similar data, plus, in the future, I might need common functionality for all the tags assigned in the application (e.g., collecting statistical data of which tags used where, or maybe for a search feature).

Now, my question is: from general engineering perspective would it be feasible to use one common intermediary/join table for all of the models consuming this 'Taggable' behavior? If so, what would be the disadvantages of doing so, and would be the best approach to tackle this.

Being new in django, I would have tried (or researched more on) these 2 scenarios if I had to do it:

SCENARIO 1:

SCENARIO 2:

Upvotes: 0

Views: 67

Answers (1)

Lyudmil Nenov
Lyudmil Nenov

Reputation: 257

I would suggest to keep your initial implementation as simple as possible (e.g. no proxy table no overwriting save) and not to worry about number of tables unless you reach some pretty amazing numbers, hit some kind of serious issues with it or or outgrow the usefulness of "simple tags". This might save you one hairy implementation that you might have not needed in the first place.

P.S. I am using https://github.com/alex/django-taggit which is pretty handy and easy to work with. Just saw that the repo has ~60 issues in gitgub :) but I have not hit any of them.

Upvotes: 1

Related Questions