Overriding update( ) Django rest framework
<p>I have a model which contains a foreign key. When I my models all fields are updated except the foreign key.</p>
<p>My models:</p>
<pre><code>class Produit (models.Model):
   titre=models.CharField(max_length=100)
   description=models.TextField()
   photo_principal=models.ImageField(upload_to='produits/',default='image.jpg')
   photo_1 = models.ImageField(upload_to='produits/', default='image.jpg')
   photo_2 = models.ImageField(upload_to='produits/', default='image.jpg')
   photo_3 = models.ImageField(upload_to='produits/', default='image.jpg')
   prix=models.FloatField()
   new_prix=models.FloatField()
   categorie=models.ForeignKey(Categorie,related_name= 'produit', on_delete=models.CASCADE)
</code></pre>
<p>serializers.py</p>
<pre><code>class ProduitUpdateSerializer(serializers.ModelSerializer):
categorie_id = serializers.PrimaryKeyRelatedField(queryset=Categorie.objects.all(),source='categorie.id')

class Meta:
    model = Produit
    fields = ['titre', 'description', 'photo_principal', 'photo_1', 'photo_2', 'photo_3', 'prix', 'new_prix',
              'categorie_id', ]

def update(self, instance, validated_data):
    print(validated_data)
    instance.categorie_id = validated_data.get('categorie_id',instance.categorie_id)
    instance.titre = validated_data.get('titre', instance.titre)
    instance.description = validated_data.get('description', instance.description)
    instance.photo_principal = validated_data.get('photo_principal', instance.photo_principal)
    instance.photo_1 = validated_data.get('photo_1', instance.photo_1)
    instance.photo_2 = validated_data.get('photo_2', instance.photo_2)
    instance.photo_3 = validated_data.get('photo_3', instance.photo_3)
    instance.prix = validated_data.get('prix', instance.prix)
    instance.new_prix = validated_data.get('new_prix', instance.new_prix)
    instance.save()

    return instance
</code></pre>