-
Notifications
You must be signed in to change notification settings - Fork 7
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
Check prepare had been called when project is called #90
base: main
Are you sure you want to change the base?
Conversation
Question -- I wonder if this enforcement should move deeper, into That function already enforces queries disabled. Perhaps it should also enforce "project only after prepare" |
Another question is -- if we detect that prepare was not called, should we then call it ourselves rather than bork? |
@pmg-certn I would say this should be added at a lower level as there is no reason as to why you would want to call I would also prefer the error rather than it magically doing it for you so you are more in control with what the code is doing, this is probably more of a point of preference rather than one being objectively better than the other though. |
|
||
def wrapped_project(qs): | ||
if not is_prepared: | ||
raise Exception("QuerySet must be prepared before projection") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course I'm not checking that the queryset they called prepare
on is actually the same one they're calling project
on...
This looks like a good start, and would certainly solve the specific issue which has come up a few times. I have one concrete concern, particulary with the suggestion that this should be done at a lower level than the view mixin, which is that I often test More generally, I can also imagine advanced use cases where the spec-generated I'm more comfortable with this checking being performed at a higher level (in the DRF view mixin), but in that case is there any benefit to the approach you've taken vs keeping the |
If a user writes DRF endpoint where they override
get_queryset()
but forget to callself.prepare(qs)
as highlighted in the documentation https://www.django-readers.org/reference/rest-framework/, then all they will see is aQueriesDisabledException
and may assume there is something wrong with theirspec
.This change tracks whether
prepare
has been called for a spec beforeproject
ia called, and if not, then we throw an exception with a specific message for this particular case.