Gaurav Rao
Gaurav Rao

Reputation: 1

Django 1.7 - update a user record using a form

I have created a form to update the existing user profile. But when i save the form it shows the error user already exists.

I used another approach by getting the user profile and then updating each field, but in that case each field has to be validated?

Any clue how to save the form as an update not as a new entry?

Upvotes: 0

Views: 103

Answers (2)

Gaurav Rao
Gaurav Rao

Reputation: 1

I managed to get the answer, i imported the form to

import the user that i want to edit

u = User.objects.get(username = user_name)

creating the form with values from existing in database and updating with values from POST

user_form = UserEditForm(request.POST,instance=u)

save the form, since it already has existing record it will update

user_form.save()

Upvotes: 0

Leistungsabfall
Leistungsabfall

Reputation: 6488

I suggest using UpdateView, one of Django's class-based-views for generic editing:

class django.views.generic.edit.UpdateView

A view that displays a form for editing an existing object, redisplaying the form with validation errors (if there are any) and saving changes to the object. This uses a form automatically generated from the object’s model class (unless a form class is manually specified).

Upvotes: 1

Related Questions