django.db.utils.Int...
 
Notifications
Clear all

django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_account.phone_number

3 Posts
2 Users
0 Reactions
65 Views
0
Topic starter
Reference Section 12 part ( Registration: Making View & Editing Model Form Clean Method to Check Passwords). 

I faced the above said error and failed to solve it. Kindly help me in this regard so that I may proceed further.

Views.py of Accounts app

```

def register(request):
    if request.method=='POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            first_name= form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            phone_number = form.cleaned_data['phone_number']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']

            username=email.split("@")[0]

            user = Account.objects.create_user(first_name = first_name, last_name = last_name, email = email, username = username, password = password)
            user.phone_number = phone_number
            user.save()

    
    else:
        form = RegistrationForm()
    context = {
        'form':form,
    }
    return render(request, 'accounts/register.html', context)

```

models.py of Accounts app

```

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager

# Create your models here.

class MyAccountManager(BaseUserManager):
    def create_user(self, first_name, last_name, username, email, password=None):
        if not email:
            raise ValueError('user must have an email address ')
        if not username:
            raise ValueError('user must have an email username ')
        
        user = self.model(
            email = self.normalize_email(email),
            username = username,
            first_name = first_name,
            last_name = last_name,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user
    
    def create_superuser(self, first_name, last_name, email, username, password):
        user = self.create_user(
            email= self.normalize_email(email),
            username = username,
            password = password,
            first_name = first_name,
            last_name = last_name,
        )

        user.is_admin = True
        user.is_active = True
        user.is_staff = True
        user.is_superadmin = True
        user.save(using=self._db)

        return user



class Account(AbstractBaseUser):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    username    = models.CharField(max_length=50, unique=True)
    email       = models.EmailField(max_length=100, unique=True)
    phone_number= models.IntegerField()

```

Register.html

```

            <form action="{% url 'register' %}" method="POST">
              {%csrf_token %}
                    <div class="form-row">
                        <div class="col form-group">
                            <label>First name</label>
                              {{form.first_name}}
                        </div> <!-- form-group end.// -->
                        <div class="col form-group">
                            <label>Last name</label>
                              {{form.last_name}}
                        </div> <!-- form-group end.// -->
                    </div> <!-- form-row end.// -->

    
                    <div class="form-row">
                        <div class="form-group col-md-6">
                            <label>Email Address</label>
                             {{form.email}}
                        </div> <!-- form-group end.// -->
                    <div class="form-group col-md-6">
                          <label>Phone Number</label>
                          {{form.phone_number}}
                        </div> <!-- form-group end.// -->
                    </div> <!-- form-row.// -->
                    <div class="form-row">
                        <div class="form-group col-md-6">
                            <label>Create password</label>
                            {{form.password}}
                        </div> <!-- form-group end.// --> 
                        <div class="form-group col-md-6">
                            <label>Repeat password</label>
                            {{form.confirm_password}}
                        </div> <!-- form-group end.// -->  
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary btn-block"> Register  </button>
                    </div> <!-- form-group// -->      
                        
                </form>
1 Answer
0

Ejaz Ali Inayat ,

The issue you're facing occurs because the phone_number field in your Account model is defined as NOT NULL. In your model, you need to allow the phone_number field to be blank. To resolve this, update your model by adding null=True to the phone_number field.

Your phone_number field should look like this:

phone_number = models.CharField(max_length=15, null=True, blank=True)

After making this change, you must run the following commands:

python manage.py makemigrations
python manage.py migrate

 

This should resolve your issue. If you need any further assistance, feel free to ask.

Ejaz Ali Inayat Ejaz Ali Inayat Topic starter 31/12/2024 12:05 am

@mohaiminul-islam A bundle of thanks Sir, issue solved

IMG 1866 rathank.com Mohaiminul - Team Rathan 31/12/2024 9:44 am

Ejaz Ali Inayat , You're most welcome .

Share: