简体   繁体   中英

Django Rest Framework - how to pass kwargs to model.save()?

I have overridden the .save() method of one of my models to use an extra argument. My question is: how I can pass this argument through the serializer.save(). My code below:

class DeliveryCreate(generics.CreateAPIView):
    queryset = Delivery.objects.none()
    serializer_class = DeliverySerializer
    permission_classes = (permissions.DjangoModelPermissions, )

    def perform_create(self, serializer):
        serializer.save(permissions=(self.request.user,) ) #here, how to I do it?

In a normal view I do it like this: self.object.save(owner=self.request.user)

I have tried: serializer.save(permissions=(self.request.user,),owner=self.request.user ) and it does not work

Thanks for any help

According to the docs serializer.save(owner=self.request.user) should be enough. The keyword arguments will be included in the validated_data argument when .create() or .update() are called on the serializer. It would help if you posted the traceback you're getting and your model/serializer code as Rahul Gupta suggested.

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