3

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?

cezar
  • 11,616
  • 6
  • 48
  • 84
taghiss
  • 115
  • 9
  • Please rephrase your question, I'm sure I'm not the only one having trouble understanding it. Do you want to ensure unique title only at creation time but not at update time? Or do you want to exclude the nested B object from the unique constraint but leave the constraint for non-nested objects? – Alex Bausk Jan 02 '18 at 15:29
  • @AlexBausk tnx i changed the question . i need to update the the instance with out already exists error. – taghiss Jan 02 '18 at 16:32
  • Possible duplicate of [Unique validation on nested serializer on Django Rest Framework](https://stackoverflow.com/questions/38438167/unique-validation-on-nested-serializer-on-django-rest-framework) – Linovia Jan 02 '18 at 23:16
  • @Linovia nope .suppose that i create an object with this titile that is unique.when i try to update the object it comes with this error :some with this title already exists.if i change the title and put request again .the update is successfully will work.but in some case i dont want to change the title .this error happens. – taghiss Jan 03 '18 at 07:55
  • @taghiss I don't see any difference. – Linovia Jan 03 '18 at 08:31
  • @Linovia if title = "title 1" and object will created in databse.for update the object in database if i try to update the object with title="title 1" .the errors comes up:some with this title already exists.how can i explain this i dont know.the logic for creating or updating is checking data base for title with that value.if title already exists comes with error.i want too exclude the title from checking.sorry for my english again. – taghiss Jan 03 '18 at 09:29
  • The create/update only applies to the root object. There's no reason this applies to any other level as they may created or updated independently. – Linovia Jan 03 '18 at 09:38
  • Are you sure that you're sending PUT request for update and POST for create? – Abhishek Dalvi May 12 '20 at 12:24

0 Answers0