회원정보 수정 form을 사용하는데 UNIQUE constraint failed: auth_user.username라는 에러가 발생합니다.

조회수 4913회

저장된 데이터를 바꾸려고합니다. 회원가입 모델은 Register이고 이 안에 있는 회원 정보 데이터를 수정할려고합니다. 지금현제 UNIQUE constraint failed: auth_user.username 에러라고 나옵니다. 이렇게 회원정보 수정할수 있을까요?

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(_("비밀번호가 일치하지 않습니다."))
            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):  # 회원정보수정
    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})

1 답변

  • 오류가 UserUpdate = super(UserUpdateForm, self).save(commit=False)여기서 발생하나요?

    어느 줄에서 오류가 발생하는지 알려주세요.


    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()
        # 기타 다른 필드 업데이트
        return render...
    

    이렇게 수정해 보시겠어요?


    그리고 함수 이름이 def signup이 맞나요? update등 다른이름이어야 할 것 같은데 햇갈리네요.

    • 죄송합니다. 오류는 user.save에서 나옵니다. def signup -> update로 수정했습니다. 알 수 없는 사용자 2016.6.2 16:38
    • 네. 오류는 user.save에서 나는군요. 그런데 답변내용대로 수정하면 잘 되시나요? 정토드 2016.6.2 22:09
    • 아.. 기타 다른 필드 업데이트에서 오류가 나네요. user.company(user_profile.cleaned_data['company']) 알 수 없는 사용자 2016.6.3 10:48
    • 아마도 user 에는 company가 없어서 오류가 나는 것같은데 'ㅁ' 천천히 풀어볼려고요. 알 수 없는 사용자 2016.6.3 10:47
    • 유저정보를 저장하는 모델을 따로 사용하고 계셨져? 그럼 set_password까지만 하고, 기타 다른 필드는 그 모델을 가져와서 업데이트 해 주셔야 합니다. 정토드 2016.6.3 11:03
    • user.email(user_profile.cleaned_data['email']) 에서 타입에러가 나오네요.'ㅁ' 이게 'str' object is not callable 알 수 없는 사용자 2016.6.3 11:20
    • 라고 나오네요. 한번 'ㅁ' 확인해볼게욥=ㅁ=/ 알 수 없는 사용자 2016.6.3 11:20
    • user.email = user_profile.cleaned_data['email']이어야 겠네요. 정토드 2016.6.3 11:26
    • 먼저는 패스워드랑 이메일은 저장은 되는데요. 다른 모델은 user_Register = Register.objects.get(user=request.user) 을 사용해서 가져오면되나요? 알 수 없는 사용자 2016.6.3 16:22

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)