Performance in Queryset CBV

Asked

Viewed 77 times

0

# models.py
class Book(TimeStampedModel):
    name = models.CharField(_('nome'), max_length=50)
    authors = models.ManyToManyField('Author', verbose_name='autores')
    price = models.DecimalField(_(u'preço'), max_digits=5, decimal_places=2)

# views.py
class BookList(ListView):
    template_name = 'core/book_list.html'
    model = Book

    def get_queryset(self):
        ''' use prefetch_related for m2m performance '''
        b = Book.objects.prefetch_related('authors').all()
        return b

You guys really think you need to prefetch_related to improve the performance of queryset? Or not need? Django’s CBV already does this automatically?

1 answer

1


To analyze the performance you can install the django-debug-toolbar:

pip install django-debug-toolbar

With it you can analyze how many darlings are made in a consultation. But the prefetch_related optimises your queries, yes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.