简体   繁体   中英

lookup_field Return None In Django get_queryset

I have a class extend with GenericAPIView ,

I Can access to the lookup_field in my functions, but I can't use it in the queryset function.

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

How Can I Access to the lookup_field in get_queryset ?

I found the answer:

Remove this line from class:

lookup_field = 'phone_number'

get the lookup_field in get_queryset, like this:

phone_number    = self.kwargs.get('phone_number')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM