I have the following model:
class Collection(models.Model):
...
relationships = models.ManyToManyField('relationships.Relationships', related_name='collections', blank=True)
...
I also have the following serializer:
class CollectionSerializer(serializers.ModelSerializer):
relationships = RelationshipSerializer(many=True)
class Meta:
model = Collection
fields = [
...
'relationships',
...
]
read_only_fields = [
...
]
def create(self, validated_data):
relationships = validated_data.pop('relationships')
# Then we need to create an instance of the Collection object from the validated data:
collection = Collection.objects.create(**validated_data)
for relationship in relationships:
relationship = Relationships.objects.get(uuid=relationship['uuid'])
collection.relationships.add(relationship.id)
return collection
A sample body I am posting to the endpoint where this serializer is called is:
{
"relationships": [
{
"name": [
"Relationship with this Name already exists."
]
},
{
"name": [
"Relationship with this Name already exists."
]
}
]
}
Even when I set the relationships field to read only it throws this error?
What have I done wrong?