Hello there, please review the model that I am defining and the SQL contents.
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Profile(models.Model):
USER_TYPE_CHOICES = (
('Donor', 'Donor'),
# first is actual value, second is human-readable placeholder
('Seeker', 'Seeker'),
)
BLOOD_GROUP_CHOICES = (
('A+','A+'),
('A-','A-'),
('AB+','AB+'),
('AB-','AB-'),
('B+','B+'),
('B-','B-'),
('O+','O+'),
('O-','O-'),
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField( max_length=15)
blood_type = models.CharField(max_length=3, choices=BLOOD_GROUP_CHOICES, blank = True, null=True)
user_type = models.CharField(max_length=10, choices=USER_TYPE_CHOICES, null=True, blank=True)
contact_number = models.IntegerField(null=True, blank=True)
def __str__(self):
return self.name
signals.py
from django.dispatch import receiver
from allauth.account.signals import user_signed_up
from .models import Profile
@receiver(user_signed_up)
def create_profile(user, **kwargs):
Profile.objects.create(user=user, name=user.username)
$ python3 manage.py sqlmigrate dashboard 0001
.
dashboard is the name of the app where models.py resides, See the SQL that it showed
BEGIN;
--
-- Create model Profile
--
CREATE TABLE "dashboard_profile" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(15) NOT NULL, "blood_type" varchar(3) NOT NULL, "user_type" varchar(10) NOT NULL, "contact_number" integer NOT NULL, "user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
COMMIT;
There are two confusions;
1. In models.py fields blood_type, user_type and contact_number are defined with null=True and blank=True. Still, CREATE TABLE defines these fields as NOT NULL. Why this?
2. Signals.py creates a new instance of Profile as; Profile.objects.create(user=user, name=user.username), clearly providing user and name fields only and not providing blood_type, user_type and contact_number. But as shown in the SQL contents, blood_type, user_type and contact_number were defined as NO NULL, why not Profile.objects.create(user=user, name=user.username) statement expects the values for blood_type, user_type and contact_number?
You can read the full story in case you are interested
Regards