Model instance doesn’t check fields Integrity

Consider the model below

class Faculty(models.Model):
     name = models.CharField(max_length=255)
     short_name = models.CharField(max_length=15)
     description = models.CharField(max_length=512)
     logo = models.ImageField(upload_to=faculty_upload_to, null=True)

That I do
Faculty.objects.create()
or

faculty = Faculty()
faculty.save()

this creates an empty entry in the database

>>> from universities.models import *
>>> Faculty.objects.create()
<Faculty: Faculty object (2)>

Why doesn’t Django give me an integrity error? I am using Django 5.0.

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it’s currently written, it’s hard to tell exactly what you’re asking.

    – 
    Bot

CharFields default to an empty string (""), which is a valid value for a VARCHAR column.

You may consider adding a CheckConstraint if you want to validate values at the database level:

class Faculty(models.Model):
    name = models.CharField(max_length=255)
    short_name = models.CharField(max_length=15)
    description = models.CharField(max_length=512)
    logo = models.ImageField(upload_to=faculty_upload_to, null=True)


    class Meta:
        constraints = [
            models.CheckConstraint(check=~Q(name="")),
            models.CheckConstraint(check=~Q(short_name="")),
            models.CheckConstraint(check=~Q(description="")),
        ]

By default, if you do not specify the value of the field, Django takes it as you are creating the object with an empty string for the field, and according to another post: Don’t allow empty string on model attribute , there is no way to tell Django to check for empty string by just indicating an attribute on the field. You either need to do manual field checking, or you can override the clean and save methods in Django models.Model class, i.e. your model:

from django.db import models
from django.db import IntegrityError

class Faculty(models.Model):
    name = models.CharField(max_length=255)
    short_name = models.CharField(max_length=15)
    description = models.CharField(max_length=512)
    logo = models.ImageField(upload_to=faculty_upload_to, null=True)

    def clean(self):
        if self.name == '' or self.short_name == '' or self.description == '':
            raise IntegrityError('No empty string allowed.')
    
    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

References: Django object validation and Django IntegrityError

from django.db import models

def faculty_upload_to(instance, filename):
    pass

class Faculty(models.Model):
    name = models.CharField(max_length=255)
    short_name = models.CharField(max_length=15)
    description = models.CharField(max_length=512)
    logo = models.ImageField(upload_to=faculty_upload_to, null=True)

    def save(self, *args, **kwargs):
        if not self.name or not self.short_name or not self.description:
            raise ValueError("Name, short_name, and description are required fields.")
        
        super().save(*args, **kwargs)

Leave a Comment