Skip to content

Don't hit db to access user_id in TokenProxy #7852

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

Conversation

zaslavskii
Copy link
Contributor

Description

When using nplusone middleware from https://github.com/jmcarp/nplusone, the error occurs if one tries to access Token app in django admin.

This happens due to recursive DB calls, as the user id is accessed through the user object rather than through user_id field.

Not sure if it's a problem of DRF itself, but the following change fixes the problem as well as reduces DB hits.

image

jmcarp/nplusone#38

@tomchristie
Copy link
Member

Okay, I actually had to look this up in the note here... https://docs.djangoproject.com/en/3.1/topics/db/models/#field-name-hiding-is-not-permitted

Some fields define extra attributes on the model, e.g. a ForeignKey defines an extra attribute with _id appended to the field name, as well as related_name and related_query_name on the foreign model.

Do the Django docs mention the <field>_id attribute elsewhere, or only in that passing note?

@zaslavskii
Copy link
Contributor Author

@tomchristie
Copy link
Member

Right, makes sense. Thanks!

@tomchristie tomchristie merged commit b25ac6c into encode:master Mar 19, 2021
@zaslavskii zaslavskii deleted the bug/dont-hit-db-when-accessing-user-id-from-token-proxy branch March 19, 2021 13:22
stefanacin pushed a commit to stefanacin/django-rest-framework that referenced this pull request Mar 22, 2021
@tomchristie tomchristie mentioned this pull request Mar 25, 2021
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
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