Updating User model in Django with class based UpdateView

社会主义新天地 提交于 2019-11-28 17:30:44
thesteve

I need to override the get_object() method on the update view and do not need to override form_valid. The custom get_object() method is:

    def get_object(self, queryset=None):
        return self.request.user

I know this is an old post but something stood out to me and this comment is info for newcomers.

The get call for self.object will work but it's not matching the actual fields to get username as it's supplying the user instance:

self.object = User.objects.get(username=self.request.user)

You should match the username argument with the instance username argument:

self.object = User.objects.get(username=self.request.user.username)

Better still, use the pk (id):

self.object = User.objects.get(pk=self.request.user.pk)

There could be a neater way of doing this, so I'm open to suggestions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!