QuerySetRequestMixin - Why can request can be None ?
#1728
yaakovLowenstein
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In
QuerySetRequestMixinthe function to get the request is:I was curios, in what scenario can the request ever actually not be there? Why not raise an error instead?
Allowing the field to be None introduces confusion. If you access the field before super().init is called in the FilterSet (where the request gets assigned), the field’s queryset will default to the full queryset. That happens because at that point the request is still None. Since None is allowed, it silently falls back to returning the full queryset.
This can be misleading and error-prone. It would be safer if the field simply didn’t allow access to its queryset when no request is available. That way, you can’t accidentally work with the unfiltered queryset.
Beta Was this translation helpful? Give feedback.
All reactions