How to update User object without creating new one?

前端 未结 2 1134
时光取名叫无心
时光取名叫无心 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条回答
  •  闹比i
    闹比i (楼主)
    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)
            ...
    

提交回复
热议问题