Я создаю пользовательскую модель с AbstractUser на django, и я получаю эту ошибку, когда создаю нового пользователя. Я использую оболочку для создания пользователя: CustomUser.objects.create(email="testmaiil2323@gmail.com", name="test name aasxs", business_name="asdkasdk sl", password="testpass234")
Я успешно создал суперпользователя с помощью команды createuperuser, но когда я создаю новых пользователей, django сообщает мне: django.db.utils.IntegrityError: Сбой ограничения NOT NULL: apirest_customuser. business_id
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _
class Business(models.Model):
name = models.CharField(max_length=150, unique=True)
def __str__(self):
return self.name
class Profile(models.Model):
name = models.CharField(max_length=150, unique=True)
def __str__(self):
return self.name
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'))
if not extra_fields.get('business_name'):
raise ValueError(_('The Business name must be set'))
if not extra_fields.get('name'):
raise ValueError(_('The Name must be set'))
if not password:
raise ValueError(_('The Passowrd must be set'))
email = self.normalize_email(email)
business = Business.objects.create(name=extra_fields.get('business_name'))
profile = Profile.objects.create(name=extra_fields.get('name'))
user = self.model(email=email, business=business, profile=profile, **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)
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
business = models.OneToOneField(Business, on_delete=models.CASCADE)
profile = models.OneToOneField(Profile, on_delete=models.CASCADE)
business_name = models.CharField(max_length=150)
name = models.CharField(max_length=300)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['business_name', 'name']
objects = CustomUserManager()
def __str__(self):
return self.email
я ожидал создать пользователя с новым профилем и бизнесом