Avoid printing/executing querysets present in a related field #7782
Unanswered
michelts
asked this question in
Potential Issue
Replies: 2 comments 4 replies
-
I'm not sure about the workflow to escalate this discussion to be an issue. Anyway, I created the pull request #7783 with a possible solution. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Your immediate solution is to implement As per the mailing list thread you posted on, this behaviour comes from Django and is very unlikely to change. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello django-rest-framework developers!
First of all, thanks for this great software!
I had a simple view that was using a serializer with a PrimaryKeyRelatedField, something like:
This is a simplified example, but the point is: I was having an error after initializing the serializer (e.g. the
do_something_with
function was raising an unexpected exception).The default Django logs setup was causing the serializer to be printed, I mean, to be converted to a string, and due to the way the printable serializer representation works, the queryset from the primary key related field was also printed and that caused some troubles.
Django has a default string representation for a queryset: if you try to print it, it will actually query 21 items from the database and print them. There is a discussion about the potential risk involved in that, as well as rejected bug reports here and here.
In my case, this specific view was querying a table with millions of rows, without a good sorting pattern. Luckily we could manage to find out what was causing performance problems and realized it was a hit to that whole table (thanks for Sentry and Datadog 😄), but this is a potential pain point that is hard to debug. I would feel more comfortable if django-rest-framework would prevent this to happen 😉
Django-rest-framework already deals with Manager instances. I'm not sure if it is because of the same reason, but I would suggest printing the
queryset.query
instead if the instance is a Queryset.I created a sample project to simulate the problem and recorded a video with details on how to reproduce it.
Beta Was this translation helpful? Give feedback.
All reactions