Reputation: 3262
I start new project with two app (customuser and client). In customuser I redefine default User model and in client make UserProfile with OneToOne field to User. Then I run
makemigartions customuser
makemigrations client
migrate
In this point all Ok Bun then I run
createsuperuser
The error is appear
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: client_profile.position_id
customuser/models.py
from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
from django.utils.translation import ugettext_lazy as _
class UserManager(BaseUserManager):
"""Define a model manager for User model with no username field."""
use_in_migrations = True
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 given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
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)
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 User(AbstractUser):
"""User model."""
username = None
email = models.EmailField(_('email address'), unique=True)
phone = models.CharField(max_length=20, blank=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
client/models.py
from django.db import models
from customuser.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Status(models.Model):
status = models.CharField(max_length=50)
class Position(models.Model):
position = models.CharField(max_length=40)
class Role(models.Model):
role = models.CharField(max_length=50)
class Company(models.Model):
company = models.CharField(max_length=50)
class Profile(models.Model):
P = 'partner'
U = 'user'
TYPE_CHOICES = (
(P, 'partner'),
(U, 'user'),
)
user = models.**OneToOneField**(User, on_delete=models.CASCADE)
user_type = models.CharField(max_length=10, choices=TYPE_CHOICES, default="U")
name = models.CharField(max_length=50, blank=True)
surname = models.CharField(max_length=50, blank=True, default = None)
avatar = models.ImageField(upload_to = 'images/profile/%Y/%m/%d/', blank=True, null=True)
position = models.ForeignKey('Position',blank=True, default=None)
role = models.ForeignKey('Role', blank=True, default=None)
company = models.ForeignKey('Company',blank=True, default=None)
status = models.ForeignKey('Status', blank=True, default=None)
@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
Help to understand why this is happening and solve this issue.
Upvotes: 0
Views: 299
Reputation: 309009
You get the error because you are creating a user without setting position
. If you allow null in the database, you need to set null=True
.
class Profile(models.Model):
...
position = models.ForeignKey('Position',blank=True, null=True)
Once you've done this, you'll need to create a migration and migrate to drop the NOT NULL constraint in your database.
Upvotes: 2