Django-allauth未在自定义SignupForm上保存其他字段

时间:2019-04-22 17:53:28

标签: django django-allauth

我无法正确扩展django-allauth注册表单。

我需要的字段显示在我的注册表单中,但是rappa_name字段未存储在我的数据库中。

我关注了这个线程:enter image description here

但是无法正常工作。

我的forms.py:

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm


User = get_user_model()


class UserCreationForm(UserCreationForm):

    rappa_name = forms.CharField(label = "Rappa Name")

    class Meta(UserCreationForm.Meta):
        model = User

    def signup(self, request, user):

        user.rappa_name = self.cleaned_data['rappa_name']
        user.save()
        return user

和我的模型。py


class User(AbstractUser):

    # raooa name of user
    rappa_name = CharField(_("Rappa Name"), default='', max_length=255)

    def get_absolute_url(self):
        return reverse("users:detail", kwargs={"username": self.username})

我确保我的settings.py有

ACCOUNT_FORMS = {
'signup': 'myproject.users.forms.UserCreationForm',
}

我需要将rappa_name与用户一起保存为字段,但当前未保存

1 个答案:

答案 0 :(得分:0)

已解决:我将rappa_name字段移至我的个人资料,然后将SignupForm更改为:

class UserCreationForm(SignupForm):

    rappa_name = forms.CharField(max_length=30, label='Rappa Name')

    def save(self, request):

        # Ensure you call the parent classes save.
        # .save() returns a User object.
        user = super(UserCreationForm, self).save(request)

        # Add your own processing here.
        profile = user.profile
        profile.rappa_name = self.cleaned_data['rappa_name']
        profile.save()
        # You must return the original result.
        return user

我在https://django-allauth.readthedocs.io/en/latest/forms.html处关注了文档。