Django 1.6: Edit Profile: column username is not unique

I'm trying to create edit user profile functionality. But I get the following error. I feel like it's trying to create a new user instead of editing the existing one. So that might result into username not being unique. I've no idea what to change/add here.

Here is my views

def showProfile(request, id):
    profile = DoctorSeeker.objects.get(id=id)
    return render(request,'meddy1/seekerprofile.html',{'doctorSeeker': profile})

def update_profile(request):
    args = {}

    if request.method == 'POST':
        form = UserUpdateForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, showProfile())

    else:
        form = UserUpdateForm()

    args['form'] = form
    return render(request, 'meddy1/editseekerprofile.html', args)

Here is my forms

class UserUpdateForm(forms.ModelForm):
    name = forms.CharField(max_length=200, widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'FirstName LastName'}))
    email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Please enter a valid email address so we can reach you. No spam. Ever.'}))

    password1 = forms.CharField(label="Old Password",widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder':'Enter your password to save the changes.'}),required=False)
    password2 = forms.CharField(label="New Password?",widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder':'Only enter new password if you want to change it.'}),required=False)
    password3 = forms.CharField(label="Confirm New Password",widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder':'Confirm New Password'}),required=False)



    class Meta:
        model = DoctorSeeker
        fields = ("name","email")

    class Meta:
        model = User
        fields = ("password2", "password3")

    def clean_password2(self):
        password2 = self.cleaned_data.get("password2")
        password3 = self.cleaned_data.get("password3")
        if password2 and password3 and password2 != password3:
            raise forms.ValidationError(
                self.error_messages['password_mismatch'],
                code='password_mismatch',
            )
        return password2

    def save(self, commit=True):
        user = super(UserUpdateForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password2"])
        fullName = self.cleaned_data["name"]
        Email = self.cleaned_data["email"]

    if commit:
        user.save()
        userProfile = DoctorSeeker(user=user, name=fullName, email=Email)
        userProfile.save()

    return user

Answers


the save method of django form will create a new object (if no instance are suplied) and not an update of this one. You have to retreive your user first pass the instance through the form and then save it.

For more informations see: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method


Need Your Help

Modify a canvas using fabric.js without removing existing content

javascript html5 canvas fabricjs

In my web page there is a canvas with some content and this content is rendered separately (without using fabric.js).

Scheme full-house poke function

list scheme lisp racket

I'm trying to define a function in Scheme that determines if a five-element list contains a full-house (that is, 3 elements are the same and the other 2 are the same, respectively). I have the outl...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.