-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Automatically determine select_related
and prefetch_related
on ModelSerializer.
#1964
Comments
It's actually been a conscious design decision to leave the Attempting to automatically add them would be helpful in many cases, but could also end up failing or giving worse performance in other cases. The other bit that's awkward is that we'd only want to add this to Of course we'd also have to do the clevernesses of figuring out when to use Having said all that, might not be averse to building something like this in, in the future. Any thoughts from anyone else? |
select_related
and prefetch_related
on ModelSerializer.
A follow up question, would be "given that DRF does not currently do the I feel at the very least more documentation is deserved. I was quite
|
Assuming you're using generic views then specify it on the
And yes, we should make to include some notes on that in the generic view section, plus also have a a section on general usage tips etc. Including:
|
Okay. That makes sense. I am curious to hear what others think, but I would love to see some magic
|
"slug related usage" That's just one of many possibilities of course - nested serializers and other relationship types also benefit from this. |
At this point in time I'm closing this off in favor of the documentation ticket #1977. |
Hi, |
@Deepakdubey90 please do use the mailing list, IRC or stack overflow for usage question. |
ok |
I'm working on this over at django-auto-prefetching I'm not quite ready to publish to PyPi yet, but so far I have an algorithm that passes tests for all the relationships(fk, m2m, 1to1) If anyone wants to try it out before I'm done publishing (maybe find out where it doesn't work and submit an issue to the repo?) I'm sharing the first version of the code here: |
First usable version of django-auto-prefetching is now live if anyone still has this problem |
Hello everyone, We faced the same issues in some projects and I have created a simple package to use in our projects. Then I have decided to publish it in github and pypi for anyone who is interested. Btw it is still being tested and there might be bugs. You can check it out on https://github.com/thetarby/django-auto-related Simply what it does: What django-rest gives us is the ability to know what will be accessed from the object before it is accessed by inspecting the source attribute of the fields defined in a serializer. Then it traces those sources back on django fields instances and translates them to For instance, If you have any feedback I would be glad to hear from you on github. |
I'm using django-auto-prefeching in my company's project. It helps a lot. Thank you. |
You're very welcome! |
I have a serializer that looks the the following:
which is causing an N +1 select issue because
select_related
is not being used to pre-query thePerson
object. This means that in order to serialize the person field when serializing aReport
, eachPerson
must be queried for itsuuid
.We likely want to use
select_related
on allSlugRelatedField
and likely all nested serializers somewhere like here: https://github.com/tomchristie/django-rest-framework/blob/e437520217e20d500d641b95482d49484b1f24a7/rest_framework/serializers.py#L570The text was updated successfully, but these errors were encountered: