Check prepare had been called when project is called#90
Check prepare had been called when project is called#90pmg-certn wants to merge 3 commits intodabapps:mainfrom
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.
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 aQueriesDisabledExceptionand may assume there is something wrong with theirspec.This change tracks whether
preparehas been called for a spec beforeprojectia called, and if not, then we throw an exception with a specific message for this particular case.