Answer the question
In order to leave comments, you need to log in
How to organize multiple ForeignKeys in one Django model?
Good day, there is an image model that, let's say, can be used in a message, in a group and in a publication, something like this:
class Image(models.Model):
group = models.ForeignKey("Group", on_delete=models.CASCADE, verbose_name="group", blank=True, null=True)
publication = models.ForeignKey("Publication", on_delete=models.CASCADE, verbose_name="publication", blank=True, null=True)
message = models.ForeignKey("Message", on_delete=models.CASCADE, verbose_name="message", blank=True, null=True)
Image.objects.create(message=some_message)
Image.objects.create(group=some_group)
Answer the question
In order to leave comments, you need to log in
Still, my solution is one of the best and incomparably better in relation to the contenttypes framework proposed above
. Here is an interesting article that describes the cons of genericrelation and recommends one of the alternatives of my version:
https://pythobyte.com/avoid-django-s- genericforeig...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question