简体   繁体   中英

Django REST Framework, working with list of objects after get_queryset

in my views.py there is ViewSet:

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    filter_class = OrderFilter
    filter_backends = (
        OrderingFilter,
        DjangoFilterBackend,
    )
    def get_queryset(self):
         ...some query...
         return products # return QuerySet object with all products in db.

Where and how can I manipulate with Project objects after filtering? With ability to take data from request. For example:

for product in products: # after pagination, filtering, etc. 
    product.price = product.price*self.request.user.discount

Thank you!

By the time you are using a ModelViewSet , you will have to override the list() method for example. The signature is list(self, request, *args, **kwargs) where as you understand, you can use the request object and manipulate your data as needed.

Let me know if you need further help!

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