lookup_field Return None 在 Django 中 get_queryset
lookup_field Return None In Django get_queryset
我有一个 class 使用 GenericAPIView 扩展,
我可以在我的函数中访问 lookup_field,但我不能在查询集函数中使用它。
class SellerItemAPIView(GenericAPIView, ListModelMixin):
serializer_class = ShopItemSerializer
permission_classes = [AllowAny]
lookup_field = 'phone_number'
def get_queryset(self):
print(self.lookup_url_kwarg) # It Return None
print(self.lookup_field) # It Return 'phone_number'
shop_items = ShopItem.objects.filter(Q(seller_id__user_id__phone_number=self.lookup_field))
return shop_items
def get(self, request, phone_number):
result = self.list(request, phone_number)
print(result.status_code)
return result
如何访问 get_queryset 中的 lookup_field?
我找到了答案:
从 class 中删除这一行:
lookup_field = 'phone_number'
获取get_queryset中的lookup_field,像这样:
phone_number = self.kwargs.get('phone_number')
我有一个 class 使用 GenericAPIView 扩展,
我可以在我的函数中访问 lookup_field,但我不能在查询集函数中使用它。
class SellerItemAPIView(GenericAPIView, ListModelMixin):
serializer_class = ShopItemSerializer
permission_classes = [AllowAny]
lookup_field = 'phone_number'
def get_queryset(self):
print(self.lookup_url_kwarg) # It Return None
print(self.lookup_field) # It Return 'phone_number'
shop_items = ShopItem.objects.filter(Q(seller_id__user_id__phone_number=self.lookup_field))
return shop_items
def get(self, request, phone_number):
result = self.list(request, phone_number)
print(result.status_code)
return result
如何访问 get_queryset 中的 lookup_field?
我找到了答案:
从 class 中删除这一行:
lookup_field = 'phone_number'
获取get_queryset中的lookup_field,像这样:
phone_number = self.kwargs.get('phone_number')