How to Solve IntegrityError at URL Column column_name cannot be null in Django Framework

Posted on

The error message specified in the title of this article is actually happened. It is happened at the time of executing admin or administrator page powered by Django framework using Python as the programming language. The error can be shown in the following image :

How to Solve IntegrityError at URL Column column_name cannot be null in Django Framework
How to Solve IntegrityError at URL Column column_name cannot be null in Django Framework

The above error is happened after setting up an application based on Django framework, installing an additional library or plugin called allauth for authentication mechanism. Below is the display of settings.py file which is representing all the library or application attached :

 

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'crispy_forms',
    'allauth',
    'allauth.account',
    'django.contrib.admindocs',
    'rest_framework',
    'rest_framework.authtoken',
    'myapps',
]

Furthermore, the allauth itself has been modified by redefining the UserAdmin class in the admin.py file as shown below :

class UserAdmin(BaseUserAdmin):
    # The forms to add and change user instances
    form = UserAdminChangeForm
    add_form = UserAdminCreationForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
    # list_display = ('email', 'admin')
    list_display = ('email','username')
    list_filter = ('email',)
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ()}),
        ('Permissions', {'fields': ()}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2')}
        ),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

Not to mention for registering it so that it can be viewed in the admin or administrator page :

admin.site.register(User,UserAdmin)

But the problem is whenever the actual process for accessing the admin page, the above error is generated and it is viewed as shown in the error page above presented.  Precisely, the error is shown whenever the process of creating a new user is being triggered by clicking the submit button upon adding a new user in the admin page. The error is triggered and it is generated because there is a field or attribute related to the user which doesn’t exist or it doesn’t precisely defined. That field is the ‘role’ field. Since the UserAdmin class is modified, and it is added a new field which is called ‘role’, as shown below in the models.py file :

class User(AbstractBaseUser):
    username = models.CharField(max_length=40, unique=True)
    first_name = models.CharField(max_length=40, blank=True)
    last_name = models.CharField(max_length=40, blank=True)
    email = models.EmailField(
        verbose_name = 'email address',
        max_length=255,
        unique=True
    )

    # a admin user; non super-user
    is_staff = models.BooleanField(default=False) 
        # a superuser
        # admin = models.BooleanField(default=False) 
   
    role = models.ForeignKey(Role)

As shown in the above snippet code, the class definition of user has one more additional field called ‘role’. It needs an additional information of the ‘role_id’ if it is going to be created upon filling the information in the add user form located in the admin page.

The solution is quite simple, just add the additional field in the add user form located in the UserAdmin class defined in the file admins.py. Below is the important line highlighted to do the actual solution :

add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2','role')}
        ),
    )

The above lines are snippet code defined in the class of UserAdmin which is valuable to be presented upon defining a user add form. The most important line is located in this line :

'fields': ('email', 'password1', 'password2','role')

The addition is in the ‘role’ part which is specifically representing the role field added to the add form of the user. Below is the different between page which doesn’t have the ‘role’ field and the page which have the ‘role’ field.

The first one is the one which is representing an add user form without the ‘role’ field :

How to Solve IntegrityError at URL Column column_name cannot be null in Django Framework
How to Solve IntegrityError at URL Column column_name cannot be null in Django Framework

The second one is the one which is representing an add user form with the ‘role’ field :

How to Solve IntegrityError at URL Column column_name cannot be null in Django Framework
How to Solve IntegrityError at URL Column column_name cannot be null in Django Framework

So, the solution for the error triggered since the User class is being modified by adding another field named ‘role’, the consequence is the add user form must also defined with an additional field named ‘role’ by adding the ‘role’ part as shown above.

Leave a Reply