I have started a django project with extending User model from AbstractUser to my CustomUser model which have foreign key relation with other model. When I try to create superuser with manage.py it does not create a superuser. It shows an error →
django.db.utils.IntegrityError: (1048, “Column ‘cid_id’ cannot be null”).
Any help will be appreciated
blog/models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth import get_user_model
from ckeditor.fields import RichTextField
# Create your models here.
class Category(models.Model):
cid = models.AutoField(primary_key=True)
category_name = models.CharField(max_length=100)
def __str__(self):
return self.category_name
class Post(models.Model):
aid = models.AutoField(primary_key=True)
image = models.ImageField(default='blog-default.png', upload_to='images/')
title = models.CharField(max_length=200)
# content = models.TextField()
content = RichTextField()
created = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
cid = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='specialization')
approved = models.BooleanField('Approved', default=False)
like = models.ManyToManyField(get_user_model(), related_name='likes')
def __str__(self):
return self.title
users/model.py
from django.db import models
from blog.models import Category
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUser(AbstractUser):
cid = models.ForeignKey(Category, on_delete=models.CASCADE)
profile_pic = models.ImageField(default='default_person.jpg', upload_to='profile_pics')
cid in the Category model is the foreign key in the Post and CustomUser model. But when I try to createsuperuser it shows the error I mentioned above. cid can not be null for the user because when a user will register an account he has to select category in the registration form. But the superuser don’t have to register account using the registration form. So how do I have to solve this problem?