Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a NOT NULL constraint on Django charfield?

Tags:

django

I'm struggling with how I can set a NOT NULL constraint on a charfield.

Currently, my model looks like this:

class Tutors(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20, blank=False)
    email = models.EmailField(max_length=254)
    birth_day = models.DateField(auto_now=False, auto_now_add=False)

    def __str__(self):
        return(self.first_name + ' ' + self.last_name)

I want to make sure that last_name and first_name can't be saved to the database as NOT NULL. Docs are confusing me a bit lol

like image 878
jarjarbinks99 Avatar asked Jan 24 '26 06:01

jarjarbinks99


2 Answers

If you don't specify null in your model class. null=False is default

Django Documentation

like image 180
Neo Ko Avatar answered Jan 25 '26 20:01

Neo Ko


blank=False will enforce that any form for this model cannot have a blank value for that field. To set NOT NULL on the underlying database column, also include null=False.

like image 41
Code-Apprentice Avatar answered Jan 25 '26 20:01

Code-Apprentice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!