I have the following Django models setup:
class Publication(models.Model): value=models.CharField(max_length=255, unique=True) class Article(models.Model): filename = models.CharField(max_length=255) publication = models.ForeignKey(Publication, on_delete=models.CASCADE) class Image(models.Model): name = models.CharField(max_length=255) articles = models.ManyToManyField(Article)
I wanted to add an 'images' property to the Publication model. I have implemented it like this
@property def images(self): images =  for article in self.article_set.all(): images += list(article.images.all()) return list(set(images))
This works. I also considered trying something like
But I don't think that I can do this because then I have to define Images above Publications, which won't work with Articles and Images.
Is there a way for me to improve this query to handle less of the filtering in application logic and more of it in the database?