O
O
Oleg Vedernikov2017-03-03 19:54:17
Django
Oleg Vedernikov, 2017-03-03 19:54:17

Is it possible to optimize the number of requests using Django ORM?

There is a model that uses Content type
models.py

class Issue(models.Model):
  body = models.TextField()
  content_type = models.ForeignKey(
      ContentType,
      limit_choices_to={'model__in': ('first_model', 'second_model'))
  object_id = models.PositiveIntegerField()
  content_object = GenericForeignKey('content_type', 'object_id')

views.py
class IssueListView(ListView):
  queryset = Issue.objects.select_related('content_type')

When iterating over the queryset, repeated queries to the database are performed.
Using prefetch_related('content_object') doesn't help.
Is it possible to perform optimization without resorting to writing a query in raw sql?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2017-03-04
@deliro

.prefetch_related('content_object')
It is he who supports the prefetch of dynamic links .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question