How to update User object without creating new one?

前端 未结 2 1135
时光取名叫无心
时光取名叫无心 2021-02-08 15:06

Following works fine in shell:

>>> from django.contrib.auth.models import User
>>> user=User.objects.get(pk=1)
>>> user.first_name = u         


        
相关标签:
2条回答
  • 2021-02-08 16:05
    if request.method == "POST":
        kwargs = { 'data' : request.POST }
        try:
            kwargs['instance'] = User.objects.get(username=request.POST['username'])
        except:
            pass
        form = UserForm(kwargs**)
        if form.is_valid():
            user = form.save(commit=False)
            ...
    
    0 讨论(0)
  • 2021-02-08 16:12

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

    form = UserForm(data=request.POST, instance=request.user)
    
    0 讨论(0)
提交回复
热议问题