Django-allauth没有在自定义SignupForm上保存额外的字段

问题描述 投票:0回答:1

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

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

我跟着这个帖子:How to customize user profile when using django-allauth

但无法让它发挥作用。

我的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

和我的models.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保存为用户的字段,但它当前没有保存

django django-allauth
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.