Here are two examples of Django models. Pay particular attention to the has_pet method.
class Person(models.Model): name = models.CharField(max_length=255) def has_pet(self): return bool(self.pets.all().only('id')) class Pet(models.Model): name = models.CharField(max_length=255) owner = models.ForeignKey(Person, blank=True, null=True, related_name="pets")
The problem is that the has_pet method always generates a request. If you do something like this.
p = Person.objects.get(id=1) if p.has_pet(): ...
Then you will really make an additional request to check if one person has a pet. This is a big problem if you need to check out several people. It will also generate queries if they are used in such patterns.
{% for person in persons %} {% if person.has_pet %} {{ person.name }} owns a pet {% else %} {{ person.name }} is petless {% endif %} {% endfor %}
This example will actually execute an additional query for each person in the set of facial queries while it displays the template.
Is there a way to do this with just one request, or at least make less than one additional request per person? Perhaps there is another way to develop this in order to avoid the problem altogether.
I thought about adding a BooleanField to Person and updating this field whenever the pet is saved or deleted. Is this really the right way?
In addition, I already have memcached setup correctly, so these requests only happen if the results are not cached yet. I want to delete queries first of all for even more optimization.
Apreche
source share