drf-spectacular
drf-spectacular copied to clipboard
Wrong warning message is being shown
I have a view which has this get_queryset(...)
method
# views.py
class PollViewSet(viewsets.ModelViewSet):
serializer_class = PollSerializer
def get_queryset(self):
raise Exception("Raise any sort of exception here.")
The idea of raise
is to ensure that the get_queryset(...)
should raise some error during the "view inspection". After this, I'm getting the following warning while trying to access the schema -
Warning #0: PollViewSet: could not derive type of path parameter "id" because it is untyped and obtaining queryset from the viewset failed. Consider adding a type to the path (e.g. <int:id>) or annotating the parameter type with @extend_schema. Defaulting to "string".
Fortunately, after adding the queryset = Poll.objects.none()
to the view, the warning disappeared.
class PollViewSet(viewsets.ModelViewSet):
serializer_class = PollSerializer
queryset = Poll.objects.none()
def get_queryset(self):
raise Exception("Raise any sort of exception here.")
I'm not sure if this is an error, but it would be better if we rephrase the warning so that the developer can update things accordingly.
This is behaving exactly as designed.
- use
queryset
attr if available to get the model. - if
queryset
not available, try to callget_queryset
and get the model in that way.
Option 1 has the least amount of surprises, which is why it is done first.
The reason this happens is because we need the type of parameter id
. That is done either through the model or specifying it prior via (OpenApiParameter
or typed urlpatterns).
Maybe expanding the warning that an empty queryset will work too makes sense. That is what I usually do, but that might not be obvious to anybody.
If you don't like the implicit nature of how this is used by DRF (get_queryset
before queryset
), this FAQ contains an entry that is specifically for your problem: https://drf-spectacular.readthedocs.io/en/latest/faq.html#my-get-queryset-depends-on-some-attributes-not-available-at-schema-generation-time
Thanks for the info :+1:
Maybe expanding the warning that an empty queryset will work too makes sense. That is what I usually do, but that might not be obvious to anybody.
Yeah, I would expect something like this :point_down:
Warning #0: PollViewSet: could not derive type of path parameter "id" because
it is untyped and obtaining queryset from the viewset failed. Consider adding
queryset=Model.objects.none() or a type to the path (e.g. <int:id>) or
annotating the parameter type with @extend_schema. Defaulting to "string".