Django Field Types

BooleanField

If you need to accept null values then use NullBooleanField instead.

CharField

To create a CharField which can be blank, specify only blank.

DateTimeField

To create a DateTimeField which can be blank, specify blank AND null.

DecimalField

For now (because I can’t find it in the docs), I am creating a DecimalField which can be blank with blank AND null (the same as an IntegerField).

FileField and ImageField

From Loading Django FileField and ImageFields from the file system:

from django.db import models

class Company(models.Model):
    name = models.CharField(max_length=100)
    logo = models.ImageField()


import requests
from django.core.files import File

from .models import Company

r = requests.get("http://media.revsys.com/img/revsys-logo.png")

with open("/tmp/revsys-logo.png", "wb") as f:
    f.write(r.content)

reopen = open("/tmp/revsys-logo.png", "rb")
django_file = File(reopen)

revsys = Company()
revsys.name = "Revolution Systems"
revsys.logo.save("revsys-logo.png", django_file, save=True)

ForeignKey

To create an optional ForeignKey, specify blank AND null e.g: mentor = models.ForeignKey(MentorModel, blank=True, null=True)

IntegerField

To create an IntegerField which can be blank, specify blank AND null.

TextField

To create a TextField which can be blank, specify only blank.