Skip to content

Error checking for nested writes #4682

@kaseyb002

Description

@kaseyb002

I believe this issue was partially addressed in #2263, but I ran into this issue again with a many to many field.

class Testy(models.Model):
    user_list = models.ManyToManyField('auth.User', related_name='just_test', blank=True)
class TestySerializer(serializers.ModelSerializer):
     user_list = serializers.SlugRelatedField(slug_field='id', many=True, queryset=User.objects.all())
     user_list_detail = UserSerializer(source='user_list', many=True, read_only=True)
     
     class Meta:
         model = Testy
         fields = (
             'id',
             'user_list',
             'user_list_detail',
)
AssertionError: The `.create()` method does not support writable nested fields by default.
Write an explicit `.create()` method for serializer `timeslots.serializers.TestySerializer`, or set `read_only=True` on nested serializer fields.

As shown, I've set the nested serializer field as read_only=True. When I delete the user_list_detail field, everything works fine. My goal is to write into the user_list field using ids, but be able to read the user list fields using UserSerializer (to get the depth).

This problem does not occur with ForeignKey fields. Additionally, reading works fine with the user_list_detail field included, but writes do not work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions