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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With