Performance in Queryset CBV

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

Do you think you really need prefetch_related to improve queryset's performance? Or do not you? Does Django's CBV automatically do this?

    
asked by anonymous 22.10.2015 / 04:35

1 answer

1

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

pip install django-debug-toolbar

With it you can analyze how many queries are made in a query. But prefetch_related does indeed optimize your queries.

    
22.10.2015 / 14:17