beda-software / drf-writable-nested

Writable nested model serializer for Django REST Framework
Other
1.07k stars 116 forks source link

Nested saving not working for deep relations #129

Open sandeepbalagopal09 opened 3 years ago

sandeepbalagopal09 commented 3 years ago
class UserSerializer(UniqueFieldsMixin, serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ("username", "email", "first_name", "last_name")

class OrganizationSerializer(WritableNestedModelSerializer):

    user = UserSerializer(source="owner.organization_user.user")

    class Meta:
        model = Organization
        fields = ("name", "user")

I have serializer like this. The user in Organization is a very deep relation. Getting error

Cannot assign "{'organization_user': {'user': OrderedDict([('username', 'username'), ('email', 'test@test.com'), ('first_name', 'Test'), ('last_name', 'Test')])}}": "Organization.owner" must be a "OrganizationOwner" instance.