Home > Net >  IntegrityError at /auth/userreg/ (1048, "Column 'user_id' cannot be null")
IntegrityError at /auth/userreg/ (1048, "Column 'user_id' cannot be null")

Time:08-16

I am a newbie in django, I am at learning stage I am creating a project appointment system where user and doctor can login . I want to register them as multiuser type for that I have extended abstract class and given one to one relation to other tables but when The code is execute and I click submit This error appears. all fields are correct there is no error in the code everything is working correct just this integrity error is not getting solved. I have not created any foriegn key just trying to create user at of the user registration .

authenticate.models

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import User
# Create your models here.

class User(AbstractUser):
    is_user = models.BooleanField('user', default=False)
    is_doctor = models.BooleanField('doctor', default=False)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

class User_reg(models.Model):
    user = models.OneToOneField('User',on_delete=models.CASCADE, primary_key=True)
    fname = models.CharField(max_length=50,blank=False)
    lname = models.CharField(max_length=50,blank=False)
    email = models.EmailField(max_length=100,blank=False)
    address = models.TextField(max_length=500,blank=False)
    gender = models.CharField(max_length=7, blank=False)
    phone = models.CharField(max_length=12,unique=True,blank=False)
    Username = models.CharField(max_length=100,blank=False,unique=True)
    Userpassword = models.CharField(max_length=100,blank=False)

views.py

from django.shortcuts import render
from django.contrib import messages
# from django.contrib.auth.models import get_user_model
from django.contrib.auth.models import User
from authenticate_me.models import User_reg, dr_reg

def login(request):
    return render(request, 'user/login.html')

def register(request):
    if request.method == "POST":
        fname = request.POST.get('fname')
        lname = request.POST.get('lname')
        email = request.POST.get('email')
        address = request.POST.get('add')
        gender = request.POST.get('sex')
        phone = request.POST.get('phone')
        username = request.POST.get('uname')
        userpassword = request.POST.get('upass')
        register = User_reg(fname=fname,lname=lname,email=email,address=address,gender=gender,phone=phone,Username=username,Userpassword=userpassword)
        register.save()

        myuser = User.objects.create_user(username, email, userpassword)
        myuser.first_name = fname
        myuser.last_name = lname
        myuser.save()

        messages.success(request, 'Register successful')
        return render(request,'user/login.html')
    else:
        return render(request, 'user/register.html')  

setting

AUTH_USER_MODEL = "authenticate_me.User"

CodePudding user response:

You should first create the User, and then the User_reg object such that you can populate the user field with the myuser object.

def register(request):
    if request.method == 'POST':
        fname = request.POST.get('fname')
        lname = request.POST.get('lname')
        email = request.POST.get('email')
        address = request.POST.get('add')
        gender = request.POST.get('sex')
        phone = request.POST.get('phone')
        username = request.POST.get('uname')
        userpassword = request.POST.get('upass')

        myuser = User.objects.create_user(
            username, email, userpassword, first_name=fname, last_name=lname
        )
        User_reg.objects.create(
            fname=fname,
            lname=lname,
            email=email,
            address=address,
            gender=gender,
            phone=phone,
            Username=username,
            Userpassword=userpassword,
            user=myuser
        )
        messages.success(request, 'Register successful')
        return redirect('login')
    return render(request, 'user/register.html')

It is however "odd" and a bit "ugly" to have two model objects, especially since you store the same information in both models. This is a form of data duplication which often eventually will make it harder to keep the data in sync.

Storing the password as plain text is also a severe security issue: Django will hash the password in the user mode with .create_user(…). That is not the case with how you store it in your model.

Furthermore I strongly advise to work with a form to process, validate and clean the user input: right now if a parameter is missing, it will error. Furthermore people can enter whatever they want: the email address does not need to be text that is formatted as an email address, and one can use any gender as "free text".

  • Related