【发布时间】:2016-11-20 22:17:26
【问题描述】:
我想创建一个 API,用户可以在其中更新他们的个人资料。就我而言,用户可以更新他/她的用户名和密码。要更改他/她的个人资料,API 链接应为/api/change/usernameOfThatUser。当我在链接中使用不存在的用户名时,我仍然得到 userProfileChange API 页面,并且输入框没有填充以前的数据。我该如何解决这个问题?
serializers.py
User = get_user_model()
class UserProfileChangeSerializer(ModelSerializer):
username = CharField(required=False, allow_blank=True, initial="current username")
class Meta:
model = User
fields = [
'username',
'password',
]
def update(self, instance, validated_data):
instance.username = validated_data.get('username',instance.username)
print('instance of username',instance.username)
return instance
views.py
class UserProfileChangeAPIView(UpdateAPIView):
serializer_class = UserProfileChangeSerializer
lookup_field = 'username'
urls.py
url(r'^change/(?P<username>[\w-]+)$', UserProfileChangeAPIView.as_view(), name='changeProfile'),
【问题讨论】:
-
当您在链接中使用不存在的用户名时,您希望发生什么?
-
你的问题没有意义。 “我该如何解决这个问题?”
-
假设我的用户名是 michael 那么我想要的是如果我想更改我的用户名然后我会做 /api/users/michael 并且页面将显示用户名字段和密码字段。在用户名字段中,我的名字将已经填写,然后我将对其进行编辑并保存。
标签: python django api django-rest-framework