Django Field Types


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


NullBooleanField is deprecated in version 3.1


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


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


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).

rate = models.DecimalField(max_digits=8, decimal_places=2)


The EmailField is a CharField so for blank, we will specify only blank.

FileField and ImageField

To create a FileField or ImageField which can be blank:

I am not sure what to do here, but we will try copying a CharField, so specify only blank.


To read from a FileField:

data = schedule.output.readlines()

File System

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("")

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

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

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


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


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


from django.core.serializers.json import DjangoJSONEncoder
parameters = JSONField(blank=True, null=True, encoder=DjangoJSONEncoder)

Date and Time

To dump json data using the DjangoJSONEncoder:

import json
from django.core.serializers.json import DjangoJSONEncoder
print(json.dumps(result, indent=4, cls=DjangoJSONEncoder))

If you add a date / time to the JSONField e.g:

my_model.parameters = {
    "from_date": from_date,
    "to_date": to_date,

You need to parse them e.g:

from dateutil.parser import parse
from_date = parse(parameters.get("from_date"))


app_group = models.ManyToManyField(AppGroup, blank=True)


I tried setting null=True, but I got the following warning from migrations:: (fields.W340) null has no effect on ManyToManyField.


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


The URLField is a CharField so for blank, we will specify only blank.


I am adding a uuid field (I couldn’t find a way to convert an existing id field):

uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

The EmailField is a CharField so for blank, we will specify only blank.

For more details, see the Django UUIDField documentation