Category id is stored as null for the user in django

I have started a django project where users have to select category while creating an account including other credentials. I have given .py file of Category, Post and CustomUser model below. The file is for the researcher-profile.html file. So when a user will login, the user will be redirected to this researcher-profile. I have set the cid in the CustomUser model as null so that I can create superuser. But for normal user, it is mandatory to select category which is why I gave specialization which is cid as required field in But the problem is when a normal user registers an account from the registration page, then in the database it stores null in the cid field even though the user selected a category while registering. How to solve this problem?


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(
    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', black=True)

    def __str__(self):
        return self.title


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, black=True, null=True)
    profile_pic = models.ImageField(default='default_person.jpg', upload_to='profile_pics')


from django import forms
from django.contrib.auth import get_user_model 
from django.contrib.auth.forms import UserCreationForm 
from blog.models import Category

User = get_user_model()

class UserRegisterForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['specialization'].required = True

    email = forms.EmailField()

    categories = Category.objects.all()
    specialization = forms.ModelChoiceField(queryset=categories)

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2', 'specialization']


{% extends 'users/base.html' %}
{% block content %}
<div class="content-section">
    <div class="media">
      <img class="rounded-circle account-img" src="{{ user.profile_pic.url }}" width="125" height="125">
      <div class="media-body">
        <h2 class="account-heading" style="margin-left: 30px">{{ user.username }}</h2>
        <p class="text-secondary" style="margin-left: 30px">{{ }}</p>
        <a href="{% url 'user-update' %}" class="btn btn-secondary btn-sm" style="margin-left: 30px;"> Edit Profile </a>
{% for post in posts %}
{% if post.approved %}
    <div class="card mb-3">
        <img class="card-img-top" src="{{ post.image.url }}" alt="Card image cap">
        <div class="card-body">
            <h5 class="card-title">{{ post.title|truncatechars:70 }}</h5>
            <p class="card-text">{{ post.content|truncatechars:200|safe }}</p>
            <a href="{% url 'post-detail' post.aid %}" class="btn btn-primary"> See Details </a>
        <div class="card-footer text-secondary">
            <a class="mr-2" href="{% url 'other-people-profile' %}">{{ }}</a>|| 
            {{ post.created|date:"F d, Y" }}
{% endif %}
{% endfor %}
{% endblock content %}
Back to Top