drf-writable-nested
drf-writable-nested copied to clipboard
Is it possible to turnoff nested creation for specific related fields ?
serializers.py
class InvoiceSerializer(WritableNestedModelSerializer):
address = AddressSerializer()
customer = CustomerSerializer()
I want to create address as the WritableNestedModelSrializer expects and allows me to do. On the other hand I don't want to create new customer while creating the Invoice. I need to simply put the id/pk of the customer as ModelSerializer allows me to do.
Here is the invoice object I want to send in the POST.
{
"customer": 2,
"address": {
"line1": "abcd- 12",
"country": "Sweden",
"city": "Stockholm",
"post_code": "12345"
}
}
can we simply do something like bellow to make it happen. serializers.py
class InvoiceSerializer(WritableNestedModelSerializer):
address = AddressSerializer()
customer = CustomerSerializer(nested=false)
I was looking for this sort of solution recently, and the best I could find was marking it read_only=True.