I realize the title sounds silly but I want to be able to change the references to the Group
objects for my User
instances. But I do not want them to be able to create new groups or edit existing groups. I think what I want is a read only nested field. However, if I set it to read_only=True
I do not get the data in my serializers validated data. If I set it to read_only=False
then it tries to create a new Group
instead of just changing the references.
class GroupSerializer(serializers.ModelSerializer):
permissions = PermissionSerializer(many=True)
class Meta:
model = Group
fields = (
'pk',
'name',
'permissions',
)
class UserSerializer(serializers.ModelSerializer):
groups = GroupSerializer(many=True)
....
class Meta:
model = User
exclude = (
....
)
def update(self, instance, validated_data):
print(validated_data)
return instance
def validate_groups(self, value):
print("validating groups")
....
return value
With read_only=True
nothing happens at all. I get the user back on my PATCH
request but the user is exactly the same. With read_only=False
I get a validation error returned to me {'groups': [{'name': ['group with this name already exists.']}]}
I have also tried overriding the create
and update
method on the GroupSerializer
but with no change.
At most, I want the GroupSerializer
just to validate that the group from the data exists.