Django REST框架 - 使用FK将对象更新为具有唯一字段的模型

时间:2015-06-10 12:26:24

标签: django-rest-framework

我有以下型号:

# models.py
class NPSUser(AbstractBaseUser, TimeStampedModel):
    email = models.EmailField(unique=True)

    first_name = models.CharField(max_length=40, blank=True)
    last_name = models.CharField(max_length=40, blank=True)

class Account(models.Model):
    user = models.OneToOneField(NPSUser, related_name='%(class)s_role', primary_key=True)

    class Meta:
        abstract = True

class Director(Account):
    tenant = models.OneToOneField(Tenant, related_name='director')

使用相应的序列化器:

# serializers.py
class NPSUserSerializer(serializers.ModelSerializer):

    password = serializers.CharField(write_only=True, required=False)
    confirm_password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = NPSUser
        fields = ('id', 'email', 'created', 'modified', 'first_name',
                  'last_name', 'password', 'confirm_password', 'director_role',
                  'manager_role', 'employee_role', 'projectmanager_role',
                  'collector_role')
        read_only_fields = ('id', 'created', 'modified', 'director_role',
                            'manager_role', 'employee_role', 'projectmanager_role',
                            'collector_role')

    def create(self, validated_data):
        return NPSUser.objects.create(**validated_data)

    def update(self, instance, validated_attrs):
        instance.email = validated_attrs.get('email', instance.email)
        instance.first_name = validated_attrs.get('first_name', instance.first_name)
        instance.last_name = validated_attrs.get('last_name', instance.last_name)

        instance.save()
        # password validation here...
        return instance

class DirectorSerializer(serializers.ModelSerializer):

    user = NPSUserSerializer()

    class Meta:
        model = Director

    def create(self, validated_data):
        user_data = validated_data.pop('user')

        user = NPSUser(**user_data)
        user.set_password(user_data['password'])
        user.save()

        director = Director.objects.create(user=user, **validated_data)

        return director

我在views.py中使用viewset.ModelViewSet,如果我已经有一个名为' John'和' email@gmail.com'数据库中的电子邮件,我发出PUT请求通过其Director关系更新NPSUser,如下所示:PUT /api/v1/directors/1/ {id: 1, tenant: 1, user: {id: 1, first_name: 'Jane', last_name: 'King', email: 'email@gmail.com'}}从服务器返回以下错误:{"user":{"email":["This field must be unique."]}},这意味着serializer.is_valid()不通过。

正确的问题是:如何在Django REST Framework中使用序列化程序从另一个与NPSUser关系字段的对象更新具有unique=True字段的NPSUser对象? / p>

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。我的解决方案是以这种方式重载电子邮件字段:

email = serializers.EmailField(unique=False)

他们实现了我自己的验证逻辑,以避免序列化程序中定义def validate(self, data)方法的重复电子邮件。在这个验证方法中,我检查数据字典中是否有id,并且,如果有,我验证电子邮件是否相同或是否已更改。我的代码是这样的。

class ClientUserCreateUpdateSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(required=False)
    email = serializers.EmailField(required=False)

    def validate(self, data):
        client_user_id = int(data.get('id', -1))
        is_update = client_user_id > 0

        if not is_update:
            if not 'email' in data:
                raise serializers.ValidationError({'email': [_('This field is required')]})
            elif ClientUser.objects.filter(is_active=True, email=data['email']).exists():
                raise serializers.ValidationError({'email': [_('The e-mail address \"%(email)s\" is already being used') %  {'email': data.get('email')}]})
        elif 'email' in data and ClientUser.objects.get(id=data['id']).email != data['email']:
            if ClientUser.objects.filter(is_active=True, email=data['email']).exists():
                raise serializers.ValidationError({'email': [_('The e-mail address \"%(email)s\" is already being used') %  {'email': data.get('email')}]})

        return data

    class Meta:
        model =  ClientUser
        fields = ('id', 'first_name', 'last_name', 'email')

希望它有所帮助。