Although we don't really want this in core, we should provide something similar to what we did in 2.x as a third party repo.
Raising as an issue here because it probably _should_ be within my remit despite being non-core.
Hi Tom,
Not sure if this is the correct place to comment, but i'm experiencing an issue with writable nested serialization.
The scenario is as follows;
class AclEntrySerializer(serializers.ModelSerializer):
class Meta:
model = models.AclEntry
class AclSerializer(serializers.ModelSerializer):
name = serializers.CharField(validators=[UniqueValidator(queryset=models.Acl.objects.all(), message='An ACL with this Label already exists')])
aclentry_set = AclEntrySerializer(required=False, many=True)
class Meta:
model = models.Acl
def create(self, validated_data):
AclEntry_data = validated_data.pop('aclentry_set')
acl = models.Acl.objects.create(**validated_data)
for acl_entry_data in AclEntry_data:
models.AclEntry.objects.create(acl=acl, **acl_entry_data)
return acl
I have 2 APIs, one for creating object Acl and AclEntry, and another for simply creating AclEntry.
The issue i'm experiencing is that when calling the AclSerializer with the nested information, the AclEntry validation is responding with acl is a required field, which is correct if calling directly, however as part of the nested serializer its a given it will be supplied once created.
Is there any approach to this other than creating 2 serializers for the AclEntry, one which excludes the foreign key?
class AclEntrySerializer(serializers.ModelSerializer):
class Meta:
model = models.AclEntry
class AclEntryNestedSerializer(serializers.ModelSerializer):
class Meta:
model = models.AclEntry
exclude = ("acl",)
class AclSerializer(serializers.ModelSerializer):
name = serializers.CharField(validators=[UniqueValidator(queryset=models.Acl.objects.all(), message='An ACL with this Label already exists')])
aclentry_set = AclEntryNestedSerializer(required=False, many=True)
class Meta:
model = models.Acl
def create(self, validated_data):
AclEntry_data = validated_data.pop('aclentry_set')
acl = models.Acl.objects.create(**validated_data)
for acl_entry_data in AclEntry_data:
models.AclEntry.objects.create(acl=acl, **acl_entry_data)
return acl
Not sure if this is the correct place to comment, but i'm experiencing an issue with writable nested serialization.
Best places to take usage questions are the discussion group or stack overflow. We won't tend to follow up on anything here which we reserve solely for dealing with triage of issues & feature requests.
+1 for this, @tomchristie have you started working this already?
I would like to offer my help, for our own project I already made something fairly generic but it's far from ready to be packaged. Let me know!
Not currently anywhere on the horizon, no.
If anything we should consider closing this as out-of-scope, given current resources.
@tomchristie I've worked a lot on writable nested model serializer and today I've published the first release of my extension which allows to create/update nested structures across serializers automatically.
See full example on https://github.com/Brogency/drf-writable-nested
I think this extension will be useful for many people and I will maintain this project for a long time because I use it in many projects.
@ruscoder That's great! Things to do are:
@asergeant01
The issue i'm experiencing is that when calling the AclSerializer with the nested information, the AclEntry validation is responding with acl is a required field, which is correct if calling directly, however as part of the nested serializer its a given it will be supplied once created.
Did you ever solve this? Even when using drf-writable-nested, it seems like I either need to create two Serializers for every model as you describe, or else set null=True on the Model. Neither feel ideal.
Most helpful comment
@tomchristie I've worked a lot on writable nested model serializer and today I've published the first release of my extension which allows to create/update nested structures across serializers automatically.
See full example on https://github.com/Brogency/drf-writable-nested
I think this extension will be useful for many people and I will maintain this project for a long time because I use it in many projects.