Overriding update( ) Django rest framework

Question!

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

My models:

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)

serializers.py

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
By : asus1902


Answers

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

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
By : Linovia


You can use conditional aggregation, something like this:

select building, date,
       (case when avg(temperature > x) > 0.1 then 'Y' else 'N' end) as flag
from t
group by building, date;


Place the console.log inside

 myService.getJSON().then(function(data){
                $scope.myData =data;
                console.log($scope.myData);
 });

DEMO



This video can help you solving your question :)
By: admin