Skip to content

Commit

Permalink
Fix n + 1 queries (#128)
Browse files Browse the repository at this point in the history
* Fix n + 1 queries
  • Loading branch information
berk76 authored Jan 9, 2025
1 parent 1f57fdd commit ce05f93
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion svjis/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def main_view(request):
def main_filtered_view(request, menu):
# Articles
q = get_article_filter(request.user)
article_list = models.Article.objects.filter(q).distinct()
article_list = models.Article.objects.select_related('author', 'menu').filter(q).distinct()

# Top 5 Articles
top_history_from = make_aware(
Expand Down
4 changes: 2 additions & 2 deletions svjis/articles/views_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def admin_company_save_view(request):
@permission_required("articles.svjis_edit_admin_company")
@require_GET
def admin_board_view(request):
board_list = models.Board.objects.all()
board_list = models.Board.objects.select_related('member')
ctx = utils.get_context()
ctx['aside_menu_name'] = _("Administration")
ctx['aside_menu_items'] = get_side_menu('board', request.user)
Expand Down Expand Up @@ -268,7 +268,7 @@ def admin_entrance_delete_view(request, pk):
@permission_required("articles.svjis_edit_admin_building")
@require_GET
def admin_building_unit_view(request):
unit_list = models.BuildingUnit.objects.all()
unit_list = models.BuildingUnit.objects.select_related('type', 'entrance')
type_list = models.BuildingUnitType.objects.all()
entrance_list = models.BuildingEntrance.objects.all()

Expand Down
4 changes: 3 additions & 1 deletion svjis/articles/views_adverts.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def get_side_menu(active_item, user):
@permission_required("articles.svjis_view_adverts_menu")
@require_GET
def adverts_list_view(request):
advert_list = models.Advert.objects.filter(published=True, created_by_user__is_active=True)
advert_list = models.Advert.objects.select_related('type', 'created_by_user').filter(
published=True, created_by_user__is_active=True
)
scope = request.GET.get('scope', 'all')
scope_description = _('All')

Expand Down
6 changes: 5 additions & 1 deletion svjis/articles/views_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def contact_view(request):
@permission_required("articles.svjis_view_phonelist")
@require_GET
def phonelist_view(request):
phone_list = User.objects.filter(is_active=True, userprofile__show_in_phonelist=True).order_by('last_name')
phone_list = (
User.objects.select_related('userprofile')
.filter(is_active=True, userprofile__show_in_phonelist=True)
.order_by('last_name')
)
ctx = utils.get_context()
ctx['aside_menu_name'] = _("Contact")
ctx['object_list'] = phone_list
Expand Down
2 changes: 1 addition & 1 deletion svjis/articles/views_faults.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_side_menu(active_item, user):
@permission_required("articles.svjis_view_fault_menu")
@require_GET
def faults_list_view(request):
fault_list = models.FaultReport.objects.all()
fault_list = models.FaultReport.objects.select_related('created_by_user', 'assigned_to_user')
scope = request.GET.get('scope', '')
if scope == 'open':
fault_list = fault_list.filter(closed=False)
Expand Down
6 changes: 3 additions & 3 deletions svjis/articles/views_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def redaction_menu_delete_view(request, pk):
@permission_required("articles.svjis_edit_article")
@require_GET
def redaction_article_view(request):
article_list = models.Article.objects.all()
article_list = models.Article.objects.select_related('author', 'menu')

# Search
search = request.GET.get('search')
Expand Down Expand Up @@ -327,7 +327,7 @@ def redaction_article_asset_delete_view(request, pk):
@permission_required("articles.svjis_edit_article_news")
@require_GET
def redaction_news_view(request):
news_list = models.News.objects.all()
news_list = models.News.objects.select_related('author')
header = _("News")

# Paginator
Expand Down Expand Up @@ -474,7 +474,7 @@ def redaction_useful_link_delete_view(request, pk):
@permission_required("articles.svjis_edit_survey")
@require_GET
def redaction_survey_view(request):
survey_list = models.Survey.objects.all()
survey_list = models.Survey.objects.select_related('author')
header = _("Surveys")

# Paginator
Expand Down

0 comments on commit ce05f93

Please sign in to comment.