The error UNIQUE constraint failed: auth_user.username occurs when using the member information modification form.

Asked 2 years ago, Updated 2 years ago, 42 views

You are about to replace the stored data. The membership model is Register and I want to modify the membership information data in it. Currently, UNIQUE constrain failed: auth_user.username It says error. Can I modify my membership information like this?

class UserUpdateForm(forms.ModelForm):
     class Meta:
            model = User
            fields = ( "email", "password1", "password2", "company", "office", )

     def clean(self):
            if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
                if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                    raise forms.ValidationError (_("PASSWORD MISCHANGE")"))
            return self.cleaned_data

      def save(self, commit=True):
            UserUpdate = super(UserUpdateForm, self).save(commit=False)
            UserUpdate.set_password=self.cleaned_data['password1']
            UserUpdate.company = self.cleaned_data['company']
            UserUpdate.office = self.cleaned_data['office'

def signup(request): # Modify member information
    if request.method == 'POST':
        user_profile = UserUpdateForm(request.POST)
        if user_profile.is_valid():
            user=user_profile.save(commit=False)
            user.username=request.user
            user.save()
            return render(request, 'as/main.html')
    else :
        user_profile = Register.objects.get(user=request.user)
        user_profile = UserUpdateForm(instance=user_profile)
    return render(request, 'registration/singup.html', {'form': user_profile})

django

2022-09-22 21:33

1 Answers

Error is UserUpdate = super(UserUpdateForm, self).save(commit=False)Does it occur here?

Please let me know which line is causing the error.

if user_profile.is_valid():
    user = request.user
    user.set_password(user_profile.cleaned_data['password1'])
    user.email = user_profile.cleaned_data['email']
    user.save()
    # Update Other Fields
    return render...

Would you like to modify it like this?

And is the function name def signup correct? I think it should be a different name, such as update, but it's different.


2022-09-22 21:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.