I have two model in my models.py
. They are related with OneToOne
relation. This is the structure:
models.py
class A (models.Model):
foo = models.CharField()
class B (models.Model):
title = models.CharField(unique=True)
some = models.OneToOneField(A, on_delete=models.CASCADE,
related_name="some")
serializers.py
class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
fields = "__all__"
class ASerializer(serializers.ModelSerializer):
some = BSerializer()
class Meta:
model = A
fields = "__all__"
def update(self, instance, validated_data):
some_data = validated_data.pop('some')
instance.some.title = some_data.get('title',
instance.some.title)
instance.foo = validated_data.get('foo', instance.foo)
a = instance.some
instance.save()
a.save()
return instance
In update call the title will be unchanged, the db validation error comes up: the title field already exists
. How do I tell django to exclude this from querying entire db?