使用基于类的 UpdateView 更新 Django 中的用户模型

发布于 2024-11-10 16:48:12 字数 813 浏览 6 评论 0原文

我正在尝试使用基于类的 UpdateView 更新 Django 用户模型,该 UpdateView 自动与当前用户一起呈现,但收到需要 pk 或 slug 的错误。该表单可以工作并使用正确的当前用户上下文进行渲染,但当我尝试提交更改时会抛出错误。下面是我正在使用的视图:

class UserUpdateView(UpdateView):
    form_class = UserForm
    model = User
    template_name = 'members/user_update.html'

    def get(self, request, **kwargs):
        self.object = User.objects.get(username=self.request.user)
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        context = self.get_context_data(object=self.object, form=form)
        return self.render_to_response(context)

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.save()
        return HttpResponseRedirect(self.get_success_url())

I am trying to update the Django User model with the class based UpdateView that automatically renders with the current user but am getting an error that a pk or slug is required. The form work and renders with the proper current user context but throws the error when I try to submit the changes. Below is the view I am using:

class UserUpdateView(UpdateView):
    form_class = UserForm
    model = User
    template_name = 'members/user_update.html'

    def get(self, request, **kwargs):
        self.object = User.objects.get(username=self.request.user)
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        context = self.get_context_data(object=self.object, form=form)
        return self.render_to_response(context)

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.save()
        return HttpResponseRedirect(self.get_success_url())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那些过往 2024-11-17 16:48:12

我需要重写更新视图上的 get_object() 方法,并且不需要重写 form_valid。自定义 get_object() 方法是:

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

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
薆情海 2024-11-17 16:48:12

我知道这是一篇旧帖子,但有一些东西对我来说很突出,这个评论是给新人的信息。

self.object 的 get 调用将起作用,但它与获取用户名的实际字段不匹配,因为它提供用户实例:

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

您应该将用户名参数与实例用户名参数匹配:

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

更好的是,使用 pk (id):

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

可以是一种更简洁的方法,所以我愿意接受建议。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文