Django GenericForeignKey for ManyToMany relationship

I'd like to enable the use of tags on different entities of my database, which have nothing to do with each other. For this, I used the GenericForeignKey and the ContentType django framework. For example, I have the 2 followings entities and the Tag class:

class Tag(models.Model):
    name = models.CharField(max_length=100)
    content_type = models.ForeignKey(ContentType, blank=True, null=True, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField(blank=True, null=True)
    content_object = GenericForeignKey("content_type", "object_id")

class Entity1 (models.Model):
    ...
    tags = GenericRelation(Tag, related_query_name="entity1")

class Entity2 (models.Model):
    ...
    tags = GenericRelation(Tag, related_query_name="entity2")

However, this design only allows to use one tag to be used for one entity, I would like a ManyToMany relationship between my Tag and the different EntityX objects. What would be the best design to achieve this ?

Back to Top