Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django filter based on request data inside perform_create

I have an API endpoint which creates an object with a specific field. I do this by using perform_create

def perform_create(self,serializer):
    group = DeviceGroup.objects.get(is_default=True, customer_uuid='some uuid')
    serializer.save(group_uuid=group)

When I hardcode the uuid of a customer, it works like a charm. However, I obviously do not want to hardcode an uuid in there.

The customer_uuid is sent in the POST request.

I tried:

  • self.kwargs['customer_uuid']
  • self.request.customer_uuid
  • self.request.GET['customer_uuid']
  • self.request.GET('customer_uuid')

How do I get the customer_uuid from the request?

EDIT

self.request.POST.get('customer_uuid') does not work when sending JSON in the request.

To fix this, use self.request.data['customer_uuid']

like image 754
Stevy Avatar asked Oct 17 '25 02:10

Stevy


1 Answers

self.request.POST.get('customer_uuid')

like image 115
Lidiya Parshina Avatar answered Oct 18 '25 15:10

Lidiya Parshina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!