DRF PUT请求唯一模型字段

时间:2016-11-17 11:37:31

标签: django django-rest-framework

我有以下型号:

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 = (
  ..................
    )
    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)

和序列化器:

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 update(self, instance, validated_data):
        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

当我尝试在不更改Movie模型中的name字段的情况下发送put请求时,会抛出错误

{
    "movie": {
        "name": [
            "movie with this name already exists."
        ]
    }
} 

但是,如果我每次都更改name字段的值,我可以完美更新任何其他字段。

1 个答案:

答案 0 :(得分:1)

问题出在您定义的电影模型中。 当您将电影模型的名称字段设置为 unique = True 时,任何具有相同电影名称的新条目将始终抛出错误。

在你的模特中,

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)

如果要添加两个具有相同名称的条目,请删除行unique = True或确保使用其他名称保存每个条目。

或者,如果您想要更新记录/条目,那么您不需要为名称字段指定值,只需从代码中删除该行,或者检查影片的名称是否与改进相同在这样的代码中:

class HirenSerializer(serializers.ModelSerializer):
    movie = MovieSerializer()

    class Meta:
        model = Hiren
        fields = ('movie', 'id', 'watched_full', 'rating', 'source', 'video_quality', 'watched_at')

    def update(self, instance, validated_data):
        movie_name = validated_data.get('movie', {}).get('name')

        if  movie_name != instance.movie.name :
            instance.movie.name = movie_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.save()

        return instance

希望这有帮助,谢谢。