Hi

> Le 30 nov. 2016 à 05:04, Ashutosh Das <[email protected]> a écrit :
> 
> Here is my model:
> 
> class Movie(models.Model):
>     name = models.CharField(max_length=800, unique=True)
>     imdb_rating = models.IntegerField(null=True)
>     movie_choice = (
>         ('Act', 'Action'),
>   ...............
>     )
>     movie_type = models.CharField(max_length=3, choices=movie_choice)
>     created_at = models.DateTimeField(auto_now_add=True)
>     updated_at = models.DateTimeField(auto_now=True)
> 
> 
> class Hiren(models.Model):
>     movie = models.ForeignKey(Movie)
>     watched_full = models.BooleanField(default=True)
>     rating = models.IntegerField()
>     source = models.CharField(max_length=500, null=True)
>     watched_at = models.DateField()
>     quality_choice = (
>         ('HD', 'HD'),
> ..............
>     )
>     video_quality = models.CharField(max_length=3, choices=quality_choice)
>     created_at = models.DateField(auto_now_add=True)
>     updated_at = models.DateField(auto_now=True)
> 
> and my serializer:
> 
> class MovieSerializer(serializers.ModelSerializer):
>     class Meta:
>         model = Movie
>         fields = '__all__'
> 
> 
> class HirenSerializer(serializers.ModelSerializer):
>     movie = MovieSerializer()
> 
>     class Meta:
>         model = Hiren
>         fields = ('movie', 'id', 'watched_full', 'rating', 'source', 
> 'video_quality', 'watched_at')
> 
>     def create(self, validated_data):
>         movie_data = validated_data.pop('movie')
>         movie = Movie.objects.create(**movie_data)
>         hiren = Hiren.objects.create(movie=movie, **validated_data)
>         return hiren
> 
>     def update(self, instance, validated_data):
>         print('hit')  # doesn't print anything on put type request
>         movie_name = validated_data.get('movie', {}).get('name')
>         # print(movie_name)
>         # print(instance.movie.name)
> 
>         if movie_name != instance.movie.name:
>             instance.movie.name = movie_name
>         # instance.movie.name = validated_data.get('movie', {}).get('name')
>         instance.movie.imdb_rating = validated_data.get('movie', 
> {}).get('imdb_rating')
>         instance.movie.movie_type = validated_data.get('movie', 
> {}).get('movie_type')
>         instance.watched_full = validated_data.get('watched_full', 
> instance.watched_full)
>         instance.rating = validated_data.get('rating', instance.rating)
>         instance.source = validated_data.get('source', instance.source)
>         instance.video_quality = validated_data.get('video_quality', 
> instance.video_quality)
>         instance.watched_at = validated_data.get('watched_at', 
> instance.watched_at)
>         # instance.movie.save()
>         instance.save()
> 
>         return instance
> 
> When I try to update the data without changing the "name" its throws an error:
> { "movie": { "name": [ "movie with this name already exists." ] } }
> 
> So I try to fix this via those lines:
> if movie_name != instance.movie.name:
>             instance.movie.name = movie_name
> 
> But it turns out that put request doesn't hit the update method at all.
There’s no reason update will be hit when the serializer doesn’t validate the 
data.

I wrote something about this kind of issue at 
https://medium.com/django-rest-framework/dealing-with-unique-constraints-in-nested-serializers-dade33b831d9
 
<https://medium.com/django-rest-framework/dealing-with-unique-constraints-in-nested-serializers-dade33b831d9>

Regards,
Xavier,
Linovia.

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to