0

Django で userena を使うのはこれが初めてです。userrena のサインアップ フォームをオーバーライドしたいのですが、エラーが発生しました。

from userena import settings as userena_settings
from userena.models import UserenaSignup
from userena.utils import get_profile_model

class SignupFormExtra(SignupForm):
"""
A form to demonstrate how to add extra fields to the signup form, in this
case adding the first and last name.


"""
cellPhone = forms.CharField(label=_(u'CellPhone'),
                             max_length=30,
                             required=False)
first_name = forms.CharField(label=_(u'First name'),
                             max_length=30,
                             required=False)

last_name = forms.CharField(label=_(u'Last name'),
                            max_length=30,
                            required=False)

def save(self):
    """
    Override the save method to save the first and last name to the user
    field.

    """
    # First save the parent form and get the user.
    new_user = super(SignupFormExtra, self).save()

    # Get the profile, the `save` method above creates a profile for each
    # user because it calls the manager method `create_user`.
    # See: https://github.com/bread-and-pepper/django-userena/blob/master/userena/managers.py#L65
    new_user = get_profile_model()

    new_user.first_name = self.cleaned_data['first_name']
    new_user.last_name = self.cleaned_data['last_name']
    new_user.cellPhone = self.cleaned_data['cellPhone']
    new_user.save(self)

    # Userena expects to get the new user from this form, so return the new
    # user.
    return new_user

実際には、model.py にクラス Profile があり、次のように userena を参照しています。

class Profile(UserenaBaseProfile):
user = models.OneToOneField(User,
                            on_delete=models.CASCADE,
                            unique=True,
                            verbose_name=_('user'),
                            related_name='my_profile')

誰かがそれを修正するのを手伝ってもらえますか?

4

1 に答える 1

1

この部分は必要ありません:

new_user = get_profile_model()

前のコマンドから new_user を取得し、それを保存して new_user.save() から self を削除します

new_user.save(self) -> new_user.save()

def save(self):
    """
    Override the save method to save the first and last name to the user
    field.

    """
    # First save the parent form and get the user.
    new_user = super(SignupFormExtra, self).save()  
    new_user.first_name = self.cleaned_data['first_name']
    new_user.last_name = self.cleaned_data['last_name']
    new_user.cellPhone = self.cleaned_data['cellPhone']
    new_user.save()

    # Userena expects to get the new user from this form, so return the new
    # user.
    return new_user
于 2016-08-13T11:38:57.217 に答える