Overriding update( ) Django rest framework
<p>i have a model wich contains a foreign Key so i create my update function but the problem when i want to update my models all fields are updated except the foreign key .I don't know why .I hope that i get an answer  </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>

<p>You shouldn't play with the id directly in that case since the serializer will return an object:</p>

<pre><code>class ProduitUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Produit
        fields = ['titre', 'description', 'photo_principal', 'photo_1', 'photo_2', 'photo_3', 'prix', 'new_prix',
                  'categorie', ]

    def update(self, instance, validated_data):
        print(validated_data)
        instance.categorie = validated_data.get('categorie', instance.categorie)
        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>

python django django-rest-framework