Django Field Types
BooleanField
If you need to accept null values then use NullBooleanField instead.
Note
NullBooleanField is deprecated in version 3.1
CharField
To create a CharField which can be blank, specify only blank.
If your CharField includes choices, then you can get the
human-readable name using get_FOO_display():
COLOUR_CHOICES = [
(GREEN, "Green"),
(RED, "Red"),
]
colour = models.CharField(max_length=30, blank=True, choices=COLOUR_CHOICES)
# In the template
{{ object.get_colour_display }}
DateTimeField
To create a DateTimeField which can be blank, specify blank
AND null.
Update
To update a created field with auto_now_add
(e.g. models.DateTimeField(auto_now_add=True):
sales_order.save()
# The 'created' field is 'auto_now' in the 'sales_order' model
# https://stackoverflow.com/questions/7499767/temporarily-disable-auto-now-auto-now-add
sales_order.created = created_at
sales_order.save(update_fields=["created"])
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).
rate = models.DecimalField(max_digits=8, decimal_places=2)
EmailField
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 onlyblank.
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 pathlib import Path
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)
Read
To read from a FileField:
data = schedule.output.readlines()
S3 / Spaces
To get the private URL for a field use the url method:
>>> x = Invoice.objects.last()
>>> x.pdf
<FieldFile: invoice/2025/11/04/001913.pdf>
>>> x.pdf.url
'https://lon1.digitaloceanspaces.com/kbsoftware/private/invoice/2025/11/04/001913.pdf?...
ForeignKey
To create an optional ForeignKey, specify blank AND null
e.g: mentor = models.ForeignKey(MentorModel, blank=True, null=True)
Filter:
qs = Ticket.objects.filter(ticket_type__isnull=True)
IntegerField
To create an IntegerField which can be blank, specify
blank AND null.
JSONField
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 = {
"contact_pk": contact.pk,
"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"))
ManyToManyField
app_group = models.ManyToManyField(AppGroup, blank=True)
Note
I tried setting null=True, but I got the following warning from
migrations:: (fields.W340) null has no effect on ManyToManyField.
TextField
To create a TextField which can be blank, specify only blank.
URLField
The URLField is a CharField so for blank, we will specify
only blank.
UUIDField
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)
To allow null:
uuid = models.UUIDField(null=True)
For more details, see the Django UUIDField documentation and Will Django UUIDFIeld behave correctly?