Django Select_Related Filter

When using select_related, someone has developed a way to filter by field in a foreign key table.

For example, given these models:

class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) class AuthorExtra(models.Model): author = models.ForeignKey(Author) type = models.ForeignKey(ExtraType) value = models.CharField(max_length = 24) 

I need a way to cache all related AuthorExtra objects of a specific type.

+4
source share
1 answer

You cannot do this with select_related , since it will only work for one-to-one or ForeignKeys fields. For feedbacks like this, the development version introduced prefetch_related , which is exactly what you are looking for.

https://docs.djangoproject.com/en/dev/ref/models/querysets/#prefetch-related

+7
source

Source: https://habr.com/ru/post/1393615/


All Articles