は、私が使用するUser Model
情報と場所の追加フィールドを見つけるために、ユーザサインアップするとき、私は必要がある設定でカスタムユーザーAllauthのカスタマイズされたSignUpフォームからカスタムユーザーモデルのフィールドを保存する方法は?
# custom_user/models.py
class MyUser(AbstractBaseUser, PermissionsMixin):
USER_ACCOUNT_TYPE_CHOICES = (
(1, _('organization')),
(2, _('personal')),
)
user_account_type = models.IntegerField(_('account type'), choices=USER_ACCOUNT_TYPE_CHOICES)
email = models.EmailField(_('email address'), max_length=255, unique=True)
USERNAME_FIELD = 'email'
class MyUserManager(BaseUserManager):
def _create_user(self, email, password, username=None, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
if username:
username = self.model.normalize_username(username)
user = self.model(username=username, email=email, **extra_fields)
else:
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email=None, password=None, username=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, username=username, **extra_fields)
def create_superuser(self, email, password, username=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, username=username, **extra_fields)
を持っています。
# settings.py
ACCOUNT_SIGNUP_FORM_CLASS = 'custom_user.forms.SignupForm'
AUTH_USER_MODEL = 'custom_user.MyUser'
私はallauth's SignupForm
を設定するには、さまざまな方法で話題をたくさん見つけましたが、誰もが働きません。この方法の一つは、以下の通りです:
リクエスト情報 POST
Variable Value
email '[email protected]'
password2 'Test1234'
csrfmiddlewaretoken 'VvrW6PcR8G8OD3fDzunXbtDh4LdYiwzs5xxaYH4xvj'
password1 'Test1234'
user_account_type '1'
username 'Test'
はそうallauthフィールドを見ているが、保存することはできません参照してブラウザで
# custom_user/forms.py
from django.forms import ModelForm
from .models import MyUser
class SignupForm(ModelForm):
class Meta:
model = MyUser
fields = ['user_account_type']
def signup(self, request, user):
user.user_account_type = self.cleaned_data['user_account_type']
user.save()
DBの値
実際のエラーは、次のとおりです。
IntegrityError at /accounts/signup/
null value in column "user_account_type" violates not-null constraint
は私def signup
正しいですか?何を設定する必要がありますか?
DjangoのV1.10、allauth v0.26.1
編集:私はworkaround
def signup(self, request, user):
user_account_type = self.cleaned_data['user_account_type']
MyUser.objects.filter(email=user).update(user_account_type=user_account_type)
を見つけましたが、この場合にはuser_account_type
もに変更する必要がありますどのようなnull=True, blank=True
を含める必要がありますこの回避策を避け、登録のすべての情報(電子メール、パスワード、アカウントタイプ)をワンステップで保存しますか?なぜallauth
はメールとパスワードを保存するときに同じフローにuser_account_type
を保存しませんか?