I have a system in which there are many relationships with the model number (say 1 a → many b), and that many models have a one-to-one relationship with another model (say 1 b → 1 c). Drawn like this:
/
I decided to create a method that collects all c that match a .
Given a model system with the same structure that I could find, it is shown in the method: Person.find_important_treats() .
Is there a better way that doesn't include so many calls to the database?
from django.db import models class Person(models.Model): """ The 'a' from my above example """ def find_important_treats(self): return (pet.treat for pet in self.pets) class Pet(models.Model): """ The 'b' from my above example """ owner = models.ForeignKey( to=Person, related_name='pets' ) favourite_treat = models.ForeignKey( to=Treat, ) class Treat(models.Model): """ The 'c' from my above example """ pass
source share