Stevy
Stevy

Reputation: 3387

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:

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']

Upvotes: 4

Views: 1132

Answers (1)

Lidiya Parshina
Lidiya Parshina

Reputation: 324

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

Upvotes: 2

Related Questions