null and blank

Date: March 20th 2016
Last updated: March 20th 2016

Null and Blank are arguments set in models.py. They both play similar roles in allowing an empty field. Null is required to allow an update to a database with an empty field. Blank is required to allow Djangos admin to update the database with an empty field.

models.py

class Beach(models.Model):
    beachname = models.CharField('Beach name', max_length=20)
    city = models.CharField(max_length=20, null=True) #<==== No blank argument
    suburb = models.CharField(max_length=20, blank=True) #<==== No null argument
    country = models.CharField(max_length=20, null=True, blank=True)
    postcode = models.IntegerField(null=True, blank=True)

    def __str__(self):
        return '{}'.format(self.beachname)

add record (works in python shell)

from surferprofile.models import *
b1 = Beach(beachname="Island Bay")
b1.save()

update record in admin

results matching ""

    No results matching ""