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:
- include a 'class' field in the Tag model above that will keep the type of ojbect that the tag is assigned to (e.g., A, B, C, or D)
- create a proxy Tag class for each of A, B, C, D (e.g., ATag, BTag, ..), override the models.Model class' 'save' method in order to update 'class' column appropriately. I would also have to customize model's manager in order to filter and properly retrieve tags that belong to that class only.
-
let each class consume its custom Tag class. I.e.,
class A(models.Model): tags = models.ManyToManyField(ATags) ...
SCENARIO 2:
- create a common intermediary model TagAssigned, and include the extra 'class' field besides the 2 foreign keys for the tag and the item tagged
-
use TagAssigned as intermediary table for all 4 classes. E.g.,
Class A(models.Model) tags = models.ManyToManyField(Tag, through='TagAssigned') ... -
override and customize the 'save' methods and the model managers of classes A, B, C, and D (as opposed to proxy models in scenario 1)
Aucun commentaire:
Enregistrer un commentaire