У меня проблема с моей пользовательской моделью, я создаю API с DjangoRestFrameork, django -allauth и Django -restauth, и когда я создаю нового пользователя с этим Json для примера моего API
{"email": "user@email.com", "first_name": "jack", "last_name": "mister", "password1": "password123", "password2": "password123", "profile_type ":" individu "," identite_number ":" 23ewfwewfwe234234 "} сохранить только электронную почту и пароль, а не мое другое поле
Мой Django Модель:
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['profile_type', 'identite_number', ]
objects = CustomUserManager()
GROUPE = 'groupe'
ASSOCIATION = 'association'
INDIVIDU = 'individu'
INDUSTRY = 'industry'
USER_PROFILE_CHOICE = [
(GROUPE, 'groupe'),
(ASSOCIATION, 'association'),
(INDIVIDU, 'individu'),
(INDUSTRY, 'industry'),
]
identificator = models.CharField(blank=True, null=True, max_length=20)
profile_type = models.CharField(
max_length=12,
choices=USER_PROFILE_CHOICE,
default=INDIVIDU,
)
identite_number = models.CharField(blank=True, null=True, max_length=20)
identity_scan = models.FileField(blank=True)
Мой Django manager
class CustomUserManager(BaseUserManager):
"""
Custom user model manager where email is the unique identifiers
for authentication instead of usernames.
"""
def create_user(self, email, password, **extra_fields):
"""
Create and save a User with the given email and password.
"""
if not email:
raise ValueError(_('The Email must be set'))
email = self.normalize_email(email)
user = self.model(email=email, profile_type=extra_fields.get('profile_type'),
identite_number=extra_fields.get('identite_number'), **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
"""
Create and save a SuperUser with the given email and password.
"""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', 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, **extra_fields)
PS: я заметил, что, когда я, например, ввожу неправильный адрес электронной почты, возникает ошибка по умолчанию Django, а не моя ValueError (_ ('E-mail должен быть установлен')
спасибо и извините за мой бас Энгли sh.