Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:03:55+00:00 2026-06-01T11:03:55+00:00

Following works fine in shell: >>> from django.contrib.auth.models import User >>> user=User.objects.get(pk=1) >>> user.first_name

  • 0

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:

  • Extending UserCreationForm to include email, first name, and last name
  • add field first_name and last_name in django-profile
  • How to create a UserProfile form in Django with first_name, last_name modifications?

EDIT:

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

form = UserForm(data=request.POST, instance=request.user)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:03:56+00:00Added an answer on June 1, 2026 at 11:03 am

    Just add on request.method == ‘post’ branch this:

    form = UserForm(data=request.POST, instance=request.user)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following python code: from django.db import models from datetime import datetime
When I run the following in an Oracle shell it works fine truncate table
When I run the following code intereactively from bash it works fine. cleartool setview
the following js works fine in FF2 but in IE6 the dropdown always selects
The following code works fine: person = {:a=>:A, :b=>:B, :c=>:C} berson = {:a=>:A1, :b=>:B1,
The following script works fine: $(#regform).validate().showErrors({username:message}); After I changed the script to the below
The following code works fine for Visual C++ 2008. However, when comes to Visual
The following code works fine #define open { #define close } #include<stdio.h> #define int
The following code works fine : std::map<int, int>& m = std::map<int, int>(); int i
I am using hibernate annotations and when I do following everything works fine sessionFactory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.