jmcarp / jmcarp/nplusone

N+1 from per-instance prefetch_related_objects() (Django) calls not detected

Open
#50 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.1k
Forks
54
PR merge metrics
No merged PRs in 30d

Description

Basically just this here:

```python
from django.db.models import prefetch_related_objects

users = list(User.objects.all())

# This is an N+1 — one query per user. nplusone should catch it but doesn't.
for user in users:
prefetch_related_objects([user], "posts")

# This is the correct bulk version. One query total.
prefetch_related_objects(users, "posts")
```

I assume people don't use `prefetch_related_objects()` that much, and that's why this didn't come up in the past. I think `prefetch_related_objects()` is super useful, so I would consider this a bug.

---

Normally I would write this issue and directly start working on submitting a PR, and maybe I still will, but I currently assume this package is not maintained anymore. Kinda a shame; it's a very nice package 😄. No shade thrown at @jmcarp though, I appreciate the work and I really needed the idea of this package and I haven't thought of just flagging N+1s on my own.

For anyone similar to me though and need an extended nplusone for Django specifically:
I found the (probable) inofficial successor [django-zeal](https://github.com/taobojlen/django-zeal), where I also submitted this bug and a possible fix. Also I messed around myself to try out how I would go about a package, which is [django-nplus1](https://github.com/oliverhaas/django-nplus1), but I do not recommend using that in production right now, only if someone wants to see how I would go about fixing the above issue.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.