python - How to update Django Rest User attribute? -


i have model:

class userprofile(models.model):      user = models.onetoonefield(user, related_name='profile')     stuff = models.textfield(default='')  user.profile = property(userprofile) 

with serializer:

class userprofileserializer(userserializer):      stuff = serializers.serializermethodfield()      def get_stuff(self, obj):         return obj.profile.stuff      def update(self, instance, validated_data):         instance.profile.stuff = validated_data.get('stuff', instance.profile.stuff)         instance.save()         return instance 

with endpoint:

class updateuserprofile(updateapiview):      model = userprofile     serializer_class = userprofileserializer     permission_classes = [permissions.isauthenticated]      def get_object(self):         return self.request.user 

when call http/put update stuff field, doesn't update. don't know why.

if update method executing , getting instance.profile in method. issue happens @

instance.profile.stuff = validated_data.get('stuff', instance.profile.stuff) instance.save() 

you need update profile instance.

instance.profile.stuff = validated_data.get('stuff', instance.profile.stuff) instance.profile.save() 

"instance.profile.save()" update profile instance.


Comments