Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate queries reported by debug_toolbar

I've recently installed debug_toolbar into my application and its reporting that queries are being ran twice.

debug_toolbar is reporting a duplicated database query.

SET SQL_AUTO_IS_NULL = 0
Duplicated 2 times.

/Users/siquick/Django/soundshelter/soundshelterapp/views.py in release(128)
  genre = [release['genre'] for release in context_dict['release']]

The code snippet from views.py is:

        release_list = Releases.objects.filter(id=release_id).values('all_artists','label_no_country','id','title','genre').annotate(cnt=Count('chartsextended'))[:1]

        context_dict['release'] = release_list

        genre = [release['genre'] for release in context_dict['release']]
        label_no_country = [release['label_no_country'] for release in context_dict['release']]
        all_artists = [release['all_artists'] for release in context_dict['release']]
        title = [release['title'] for release in context_dict['release']]

What is causing this duplication? I understand that it is caused by this line genre = [release['genre'] for release in context_dict['release']] but not sure why this is happening.

like image 599
Franco Avatar asked Sep 18 '15 22:09

Franco


1 Answers

It happens because, I guess, "genre" is a model too.

You should use select_related('genre') in order to fetch them only once. https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related

like image 126
John Smith Avatar answered Oct 14 '22 00:10

John Smith