0

プロフィールの更新ページを作成しました。したがって、フィールドは次のとおりです。電子メール、名、姓。

検証では、ログに記録されたユーザーのメール アドレスを除外しようとしています。また、他のユーザーのメール アドレスをフィルター処理しています。コードを見れば何を言っているのか分かると思います。

ここでいくつかの質問を読みましたが、何かが見つかりませんでした。一部のユーザーは同じ問題を抱えていました。

したがって、以下のコードでは、 type object 'User' has no attribute 'email'という問題が発生します。

(フォームを保存する前に)現在のユーザーのメールアドレスを取得するために多くの方法を試しましたが、まだ何もありません。

フォーム.py

class UpdateProfile(forms.ModelForm):
    email = forms.EmailField(required=True)
    first_name = forms.CharField(required=False)
    last_name = forms.CharField(required=False)

    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name')

    def clean_email(self):
        email = self.cleaned_data.get('email')
        current_user_email = User.email
        if User.objects.filter(email__iexact=email).exclude(email__iexact=current_user_email).count() > 0:
            raise forms.ValidationError('This email address is already in use.'
                                        'Please supply a different email address.')
        return email

    def save(self, commit=True):
        user = super(UpdateProfile, self).save(commit=False)

        if commit:
            user.save()

        return user
4

1 に答える 1