title: Django Rest Framework: Patching a OneToOneField
body: <p>I need help with a PATCH request using Django rest framework.</p>
  
  <p>I have a <code>User</code> model that inherits from <code>AbstractBaseUser</code> which has 2 fields: <code>name</code> and <code>email</code>. The <code>email</code> field is unique.</p>
  
  <p>Then I have a <code>DojoMaster</code> model that has a OneToOne relationship with the <code>User</code> model:</p>
  
  <p><strong><code>models.py</code></strong></p>
  
  <pre><code>class DojoMaster(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
      phone = models.BigIntegerField()
      username = models.CharField(max_length=100, unique=True)
      country = models.ForeignKey(Country, on_delete=models.CASCADE)
  </code></pre>
  
  <p>I am able to POST a "dojomaster" to the application. Let's say the "dojomaster" POST looks like this:</p>
  
  <p><strong><code>POST payload</code></strong></p>
  
  <pre><code>{
      "user": {
          "name": "XYZ",
          "email": "xyz@mail.com",
          "password": "2He$8Mv*"
      },
      "phone": 2685211,
      "country": 575,
      "username": "iAmXyZ"
  } 
  </code></pre>
  
  <p>Now the "dojomaster" wants to change some of these details, so a PATCH request is sent:</p>
  
  <p><strong><code>PATCH payload</code></strong></p>
  
  <pre><code>{
      "user": {
          "name": "XYZ",
          "email": "xyz24@mail.com", #change email
          "password": "p@55w0rd" #change password
      },
      "phone": 3972925, #change phone number
      "country": 575,
      "username": "iAmXyZ"
  } 
  </code></pre>
  
  <p>To achieve this, I created the following in my <strong><code>serializers.py</code></strong>:</p>
  
  <pre><code>class UserSerializer(serializers.ModelSerializer):
      class Meta:
          model = User
          fields = ('name', 'email', 'password')
  
  class DojoMasterUpdateSerializer(serializers.ModelSerializer):
      user = UserSerializer(required=True)
  
      def update(self, instance, validated_data):
          user_data = validated_data.pop('user')
          user = User.objects.update(**user_data)
          instance.user = validated_data.get('user', user)
          instance.username = validated_data.get('username', instance.username)
          instance.phone = validated_data.get('phone', instance.phone)
          instance.country = Country.objects.get(
              country=validated_data.get('country', instance.country)
          )
          instance.save()
          return instance
  
      class Meta:
          model = DojoMaster
          fields = ('user', 'country', 'phone', 'username')
          write_only_fields = ('password',)
  </code></pre>
  
  <p>To use the serializers, I created the following view:</p>
  
  <p><strong><code>views.py</code></strong></p>
  
  <pre><code>class DojoMasterUpdateView(generics.UpdateAPIView):
      queryset = DojoMaster.objects.all()
      serializer_class = DojoMasterUpdateSerializer
  </code></pre>
  
  <p>However, when I do this, I get a <code>Status: 400 Bad Request</code> error with the following payload:</p>
  
  <pre><code>{
      "user": {
          "email": [
              "user with this email already exists."
          ]
      }
  }
  </code></pre>
  
  <p>I also tried doing with using a PUT request to no success.</p>
  
  <p>How do I PATCH an entity with this type of OneToOneField relationship using DRF? Your help will be much appreciated. </p>