我有以下序列化器:
class ProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Profile
fields = (
'pk',
'user',
'isAdmin',
'isAccountant',
'isAuditor',
'isManager'
)
def update(self, instance, validated_data):
instance.isAdmin = validated_data['isAdmin']
instance.isAccountant = validated_data['isAccountant']
instance.isAuditor = validated_data['isAuditor']
instance.isManager = validated_data['isManager']
user = validated_data.pop('user')
u = User.objects.get(email=user['email'])
u.first_name = user['first_name']
u.last_name = user['last_name']
u.username = user['email']
u.email = user['email']
u.save()
return instance
当我发送PUT请求时,作为响应,我得到了更新的数据。但是在数据库中,它保持不变。
我在做什么错了?
答案 0 :(得分:1)
您没有保存instance
:
def update(self, instance, validated_data):
instance.isAdmin = validated_data['isAdmin']
instance.isAccountant = validated_data['isAccountant']
instance.isAuditor = validated_data['isAuditor']
instance.isManager = validated_data['isManager']
instance.save()
user = validated_data.pop('user')
u = User.objects.get(email=user['email'])
u.first_name = user['first_name']
u.last_name = user['last_name']
u.username = user['email']
u.email = user['email']
u.save()
return instance