Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix n + 1 queries #128

Merged
merged 4 commits into from
Jan 9, 2025
Merged

Fix n + 1 queries #128

merged 4 commits into from
Jan 9, 2025

Conversation

berk76
Copy link
Member

@berk76 berk76 commented Jan 7, 2025

No description provided.

@berk76 berk76 marked this pull request as ready for review January 7, 2025 12:44
@SukiCZ SukiCZ self-requested a review January 7, 2025 20:11
@berk76 berk76 changed the title Fix n + 1 query on Building Units list Fix n + 1 queries Jan 7, 2025
@SukiCZ
Copy link
Collaborator

SukiCZ commented Jan 7, 2025

Pokud by QuerySety ve Views začali být obšírnější, tak doporučuji začít používat Model Managers

# managers.py
from django.db import models


class ArticleManager(models.Manager):
    def for_list_view(self):
        return self.select_related("author", "menu")


class AdvertManager(models.Manager):
    def for_list_view(self):
        return self.filter(
            published=True, created_by_user__is_active=True
        ).select_related("type", "created_by_user")


# models.py
from . import managers


class Article(models.Model):
    ...

    objects = managers.ArticleManager()

+ Jsem si všiml, že po select_related(...) tam máš zbytečné .all()

BTW Sentry umí reportovat N+1 Queries

@berk76
Copy link
Member Author

berk76 commented Jan 7, 2025

Ty model managers vypadají zajímavě - díky za link na dokumentaci.

Copy link

sonarqubecloud bot commented Jan 7, 2025

@berk76 berk76 merged commit ce05f93 into master Jan 9, 2025
9 checks passed
@berk76 berk76 deleted the fix/building-units-n-plus-1-query branch January 9, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants