get_queryset 未调用 ListCreateAPIView
get_queryset for ListCreateAPIView not being called
我有一个 ListCreateAPIView,我正试图覆盖它 get_queryset,但它从未被调用过。这是我的观点:
class DeviceView(generics.ListCreateAPIView):
def get_queryset(self):
# Threw this and some print statements, but no sign of
# the exception or print statement
raise Exception
return None
@swagger_auto_schema(
responses={
201: DeviceSerializer(),
},
request_body=DeviceSerializer,
)
def post(self, request, format=None):
# This code works fine
...
@swagger_auto_schema(responses={200: DeviceSerializer(many=True)})
def get(self, request, format=None):
# This code DOES get hit and successfully retrieves all the devices
这里是 urls.py:
urlpatterns = [
path(r"devices/<serialnumber>/abc", views.AbcView.as_view()),
path(r"devices/<serialnumber>", views.DeviceDetailView.as_view()),
path(r"devices/", views.DeviceView.as_view()),
path(r"api-auth/", include("rest_framework.urls", namespace="rest_framework")),
path(
r"swagger/",
schema_view.with_ui("swagger", cache_timeout=0),
name="schema-swagger-ui",
),
url(
r"^swagger(?P<format>\.json|\.yaml)$",
schema_view.without_ui(cache_timeout=0),
name="schema-json",
),
url(r"^api-token-auth/", obtain_jwt_token),
path("admin/", admin.site.urls),
]
关于为什么 get_queryset
不是 hit/overridden 的任何想法?
来自django rest框架代码
class ListAPIView(mixins.ListModelMixin,
GenericAPIView):
"""
Concrete view for listing a queryset.
"""
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
所以首先,最好覆盖 list
函数而不是 get
。
class ListModelMixin(object):
"""
List a queryset.
"""
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
所以您可能错过了列表中的超级调用或 self.filter_queryset 调用。这些将确保 get_queryset 被调用。
我有一个 ListCreateAPIView,我正试图覆盖它 get_queryset,但它从未被调用过。这是我的观点:
class DeviceView(generics.ListCreateAPIView):
def get_queryset(self):
# Threw this and some print statements, but no sign of
# the exception or print statement
raise Exception
return None
@swagger_auto_schema(
responses={
201: DeviceSerializer(),
},
request_body=DeviceSerializer,
)
def post(self, request, format=None):
# This code works fine
...
@swagger_auto_schema(responses={200: DeviceSerializer(many=True)})
def get(self, request, format=None):
# This code DOES get hit and successfully retrieves all the devices
这里是 urls.py:
urlpatterns = [
path(r"devices/<serialnumber>/abc", views.AbcView.as_view()),
path(r"devices/<serialnumber>", views.DeviceDetailView.as_view()),
path(r"devices/", views.DeviceView.as_view()),
path(r"api-auth/", include("rest_framework.urls", namespace="rest_framework")),
path(
r"swagger/",
schema_view.with_ui("swagger", cache_timeout=0),
name="schema-swagger-ui",
),
url(
r"^swagger(?P<format>\.json|\.yaml)$",
schema_view.without_ui(cache_timeout=0),
name="schema-json",
),
url(r"^api-token-auth/", obtain_jwt_token),
path("admin/", admin.site.urls),
]
关于为什么 get_queryset
不是 hit/overridden 的任何想法?
来自django rest框架代码
class ListAPIView(mixins.ListModelMixin,
GenericAPIView):
"""
Concrete view for listing a queryset.
"""
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
所以首先,最好覆盖 list
函数而不是 get
。
class ListModelMixin(object):
"""
List a queryset.
"""
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
所以您可能错过了列表中的超级调用或 self.filter_queryset 调用。这些将确保 get_queryset 被调用。