如何更新用户对象而不创建新的? [英] How to update User object without creating new one?

查看:231
本文介绍了如何更新用户对象而不创建新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容适用于shell:

 >>>来自django.contrib.auth.models import User 
>>> user = User.objects.get(pk = 1)
>>> user.first_name = u'Some'
>>> user.last_name = u'Name'
>>> user.save()
>>> user.first_name
u'Some'
>>> user.last_name
u'Name'

然后我尝试对表单做同样的事情:

 #forms.py 
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name','last_name']


#views.py
def edit_names(request,template_name =registration / edit_names 。
如果request.method ==POST:
form = UserForm(data = request.POST)
如果form.is_valid():
user = form.save(commit = False)
user.save()
url = urlresolvers.reverse('my_account')
返回HttpResponseRedirect(url)
else:
form = UserForm(instance = request.user)
page_title = _('编辑用户名')
返回render_to_response(template_name,locals(),
context_instance = RequestContext(request))

#edit_names.html
< form action =。 method =post> {%csrf_token%}
< table>
{{form.as_table}}
< tr>< td colspan =2>
< input type =submit/>
< / td>< / tr>
< / table>
< / form>

我在浏览器中打开页面,看到两个字段名字姓氏。当我填写字段并提交表单时,我会收到错误:

 异常类型:IntegrityError 
异常值:列用户名不唯一

我还尝试添加 ['username'] 到用户窗体中的字段列表。如果我使用我的用户名(作为request.user)提交表单,则表单显示errormessage:

 具有此用户名的用户已存在。 

如果我将用户名更改为某个唯一的名称,则会创建具有该用户名的新用户。 / p>

问题是:
如何更新User对象,而不是创建新的?



对不起,如此冗长,但是我在这里搜索不了,找不到我的问题的答案。



BTW,这些情况不为我工作:





编辑:



根据建议@fceruti我刚刚添加request.method =='post'分支这个:

  form = UserForm(data = request.POST,instance = request.user)


解决方案

只需添加request.method =='post'branch this:

  form = UserForm(data = request.POST,instance = request.user)


Following works fine in shell:

>>> from django.contrib.auth.models import User
>>> user=User.objects.get(pk=1)
>>> user.first_name = u'Some'
>>> user.last_name = u'Name'
>>> user.save()
>>> user.first_name
u'Some'
>>> user.last_name
u'Name'

Then I try to do the same with forms:

# forms.py
class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['first_name', 'last_name']


# views.py
def edit_names(request, template_name="registration/edit_names.html"):
    if request.method == "POST":
        form = UserForm(data=request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.save()
            url = urlresolvers.reverse('my_account')
            return HttpResponseRedirect(url)
    else:
        form = UserForm(instance=request.user)
    page_title = _('Edit user names')
    return render_to_response(template_name, locals(),
        context_instance=RequestContext(request))

# edit_names.html
<form action="." method="post">{% csrf_token %}
    <table>
        {{ form.as_table }}
        <tr><td colspan="2">
            <input type="submit" />
        </td></tr>
    </table>
</form>

I open page in browser and see two fields First name and Last name. When I fill in the fields and submit the form I get the error:

 Exception Type:    IntegrityError
Exception Value:    column username is not unique

I also tried to add ['username'] to fields list in UserForm. If I submit the form with my username (as request.user), the form displays errormessage:

User with this Username already exists.

If I change the username to some unique name, the new user with that username is being created.

The question is: How can I update User object, not create new one?

Sorry for being so verbose, but I had hard search here and could not find the answer to my question.

BTW, these cases don't work for me:

EDIT:

As suggested @fceruti I just added on request.method == 'post' branch this:

form = UserForm(data=request.POST, instance=request.user)

解决方案

Just add on request.method == 'post' branch this:

form = UserForm(data=request.POST, instance=request.user)

这篇关于如何更新用户对象而不创建新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆