drf-writable-nested icon indicating copy to clipboard operation
drf-writable-nested copied to clipboard

Is it possible to turnoff nested creation for specific related fields ?

Open Shrek53 opened this issue 6 years ago • 1 comments

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)

Shrek53 avatar Jan 02 '20 08:01 Shrek53

I was looking for this sort of solution recently, and the best I could find was marking it read_only=True.

martyzz1 avatar Jan 03 '20 19:01 martyzz1