0

I've been trying creating a user profile form using built-in User of django.contrib.auth.models. Everything is working fine but after filling the fields into the form(which is displaying), I am encountering an INTEGRITY ERROR AT / saying NOT NULL CONSTRAINT failed. You can see this image using this link to know exactly what the error is showing.

This is my models.py file

from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator
# Create your models here.
class UserProfileInfo(models.Model):

    user = models.OneToOneField(User,on_delete=models.CASCADE, null=True)
    phone_number = models.PositiveIntegerField(validators= 
    [MaxValueValidator(9999999999)],blank=True)


    def __str__(self): #This will print out this model
         return self.user.username

This is my forms.py file.

from django import forms
from django.contrib.auth.models import User
from Login_Signup_Form.models import UserProfileInfo

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

class Meta:   
    model=User
    fields=('first_name','last_name','username','email','password',)

    class UserProfileForm(forms.ModelForm):
        class Meta:
             model=UserProfileInfo    #this is model
             fields=('phone_number',)

This is my views.py file.

from django.shortcuts import render
from Login_Signup_Form.forms import UserForm,UserProfileForm
from Login_Signup_Form.models import UserProfileInfo
# Create your views here.

def index(request):
   return render(request,'base.html')

def register(request):
   registered=False

   if request.method == 'POST':
      user_form = UserForm(data=request.POST)  
      user_phone_number=UserProfileForm(data=request.POST)

        if user_form.is_valid() and user_phone_number.is_valid():
            user=user_form.save()
            user.set_password(user.password)
            user.save()

            phone = user_phone_number.save()
            phone.user=user    
        else:
            #Printing the errors
            print(user_form.errors,user_phone_number.errors)
   else:
        user_form = UserForm()
        user_phone_number = UserProfileForm()


  return render(request, 'base.html',{'user_form':user_form, ' 
phone_number':user_phone_number})
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
VAIBHAV GUPTA
  • 51
  • 1
  • 4

1 Answers1

0

The error probably comes from an empty phone number in your form. You allow an empty phone_number in your form with blank=True but you don't allow it on the database level, you need to add null=True as well:

phone_number = models.PositiveIntegerField(validators= 
[MaxValueValidator(9999999999)], blank=True, null=True)

See this great answer.

With blank=True the field is not required and the form will validate but it will raise the integrity error because null=True is not here. That wouldn't happen with a CharField though, the blank value would be stored as empty string. This only happens because of the PositiveIntegerField.

Cyrlop
  • 1,894
  • 1
  • 17
  • 31
  • With the field being required, the form wouldn't validate if the field wasn't filled, so it wouldn't even hit the database. – bruno desthuilliers Aug 14 '18 at 10:04
  • With `blank=True` the field is not required and the form will validate but it will raise the integrity error because `null=True` is not here (that wouldn't happen with a `CharField` though, the blank value would be stored as empty string). I think this only happens because of the `PositiveIntegerField`. Please check [this](https://stackoverflow.com/a/8609425/2528658). If I'm correct, please remove the downvote :) – Cyrlop Aug 14 '18 at 10:58
  • you're right indeed, I should have read more carefully, my bad :-/ Could you please edit your post so I can undownvote it ? – bruno desthuilliers Aug 14 '18 at 11:14