4๐
โ
You might consider generic relations here.
class Annotation(models.Model):
body = models.TextField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
annotated_object = GenericForeignKey('content_type', 'object_id')
class Book(models.Model):
...
annotations = GenericRelation('Annotation')
class Article(models.Model):
...
annotations = GenericRelation('Annotation')
now you can set annotation.annotated_object
to any book or article instance, and in reverse do obj.annotations
from both book and article objects.
๐คDaniel Roseman
Source:stackexchange.com