Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 在一个updateview中保存两个模型实例_Django_Django Rest Framework - Fatal编程技术网

Django 在一个updateview中保存两个模型实例

Django 在一个updateview中保存两个模型实例,django,django-rest-framework,Django,Django Rest Framework,我试图在一个视图中更新用户模型和用户配置文件模型,但它不起作用。不会显示任何错误,也不会对对象进行任何更改。我做得不对的是什么。 这是我的models.py: class UserProfile(models.Model): """User information not related to authentication""" user = models.OneToOneField(User, related_name='user_profile', on_delete=mod

我试图在一个视图中更新用户模型和用户配置文件模型,但它不起作用。不会显示任何错误,也不会对对象进行任何更改。我做得不对的是什么。 这是我的models.py:

class UserProfile(models.Model):
    """User information not related to authentication"""
    user = models.OneToOneField(User, related_name='user_profile', on_delete=models.CASCADE)
    age = models.IntegerField()
    # other fields ignored
这是我的serializer.py:

class UserSerializer(ModelSerializer):
    first_name = CharField(max_length=20)
    last_name = CharField(max_length=20)
    email = EmailField(required=True, validators=[UniqueValidator(queryset=User.objects.all())])
    username = CharField(max_length=32,validators=[UniqueValidator(queryset=User.objects.all())])
    password = CharField(min_length=8, write_only=True)
    confirm_password = CharField(write_only=True)


    def create(self, validated_data):
        user = User.objects.create_user(
            validated_data['username'],
            email = validated_data['email'],
            first_name = validated_data['first_name'],
            last_name = validated_data['last_name']
            )
        password = validated_data['password']
        confirm_password = validated_data['confirm_password']

        if password != confirm_password:
            raise ValidationError({'password': 'Passwords must match'})
        else:
            user.set_password(password)
            user.save()
            return user

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password', 'confirm_password')
这里是view.py:

class UserProfileUpdate(UpdateAPIView):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer
    lookup_field = 'user'
@埃里克 尝试将更新方法更改为此,实际更新数据位于
validated\u data['user']

def更新(自身、实例、已验证的_数据):
user=instance.user
instance.user.username=已验证的_数据['user'].get('username',instance.user.username)
instance.user.email=已验证的_数据['user'].get('email',instance.user.email)
instance.user.first\u name=已验证的\u数据['user'].get('first\u name',instance.user.first\u name)
instance.user.last_name=已验证的_数据['user'].get('last_name',instance.user.last_name)
save()实例
user.save()
返回实例

您对instance.user进行更改,但调用instance.save()。您还应该调用instance.user.save(),但是什么也没有发生。虽然user和instance.user应该在内存中引用同一个对象,但为了一致性,最好只使用一个对象。因此,请尝试更新和保存用户或实例。用户,这样事情就不会混淆。我的问题是,保存/更新没有发生。
class UserProfileUpdate(UpdateAPIView):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer
    lookup_field = 'user'