Basic Cheatsheet for Django Models
Updated 10 Oct 2022
Sometimes it is useful to have a condensed version of highly detailed information.
This resource is intended mainly for folks who are regularly building out models, are comfortable with the concepts of django models, and want a consolidated listing of the majority of things available for models. I use this quite often, and it saves me a lot of time since I generally don't need to look up the full documentation. I can see at a glance what a particular field, Meta class option, QuerySet, etc should look like.
I've previously posted prior versions of this elsewhere, so you may have seen something similar, but this is the latest and most up-to-date version.
import uuid
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.postgres.fields import (
ArrayField,
CICharField,
CIEmailField,
CITextField,
HStoreField,
IntegerRangeField,
BigIntegerRangeField,
DecimalRangeField,
DateTimeRangeField,
DateRangeField,
)
from django.contrib.postgres import ExclusionConstraint
def file_directory_path(instance, filename):
return f"my_files/{instance.id}/{timezone.now().date().strftime('%Y/%m')}/{filename}"
def create_rand_value(app_label, model_name, prefix=None, suffix_len=10):
"""
Just an example function, used to demonstrate overriding save()
It generates an easily readable, unique string
"""
code = ""
if prefix is not None:
code = code.join(prefix).join("-")
while True:
code = code.join(random.SystemRandom().choice("23456789BCDFGHJKMNPQRSTVWXYZ") for _ in range(suffix_len))
Model = apps.get_model(app_label=app_label, model_name=model_name)
try:
present = Model.objects.first()
except Model.DoesNotExist:
present = None
if present is not None:
if not Model.objects.filter(code=code).exists():
return code
class MyModelQuerySet(models.QuerySet):
"""Used to return a QuerySet that has been filtered"""
def expired_items(self):
return self.filter(expired__isnull=True)
class MyModelManager(models.Manager):
"""Used for table-level methods that do NOT return QuerySet"""
def delete_expired_items(self):
self.expired_items().delete()
def get_queryset(self):
return super().get_queryset().filter(critical=True)
class MyModel(models.Model):
"""Comment about this model"""
class CompanyType(models.TextChoices):
PUBLIC_LIMITED_COMPANY = 'PLC', _('Public Limited Company')
PRIVATE_COMPANY_LIMITED = 'LTD', _('Private Company limited by shares')
LIMITED_LIABILITY_PARTNERSHIP = 'LLP', _('Limited Liability Partnership')
__empty__ = _('(Unknown)')
boolean_field = models.BooleanField(_("Boolean"), default=False)
char_field = models.CharField(_("Character"), max_length=100, blank=True)
char_field_choice = models.CharField(_("Character Choices"), choices=CompanyType.choices, default=CompanyType.__empty__, max_length=100, blank=True)
date_field = models.DateField(_("Date"), null=True, blank=True)
datetime_field = models.DateTimeField(_("DateTime"), null=True, blank=True)
decimal_field = models.DecimalField(_("Decimal"), max_digits=5, decimal_places=2, null=True)
duration_field = models.DurationField(_("Duration"), )
email_field = models.EmailField(_("Email"), max_length=254, blank=True)
file_field = models.FileField(_("File"), upload_to=file_directory_path, max_length=100)
float_field = models.FloatField(_("Float"), null=True)
generic_ip_address_field = models.GenericIPAddressField(_("Generic IP"), protocol='both', unpack_ipv4=False, null=True)
image_field = models.ImageField(_("Image"), upload_to=file_directory_path, height_field=None, width_field=None, max_length=100)
integer_field = models.IntegerField(_("Number"), null=True)
json_field = models.JSONField(_("JSONField"), default=dict)
pos_int_field = models.PositiveIntegerField(_("Positive Integer"), )
pos_small_int_field = models.PositiveSmallIntegerField(_("Positive Small Integer"), )
slug_field = models.SlugFiels(_("Slug"), max_length=50, blank=True)
small_int_field = models.SmallIntegerField(_("Small Integer"), )
text_field = models.TextField(_("Text"), blank=True)
time_field = models.TimeField(_("Time"), null=True, blank=True)
url_field = models.URLField(_("URL"), max_length=200,)
uuid_field = models.UUIDField(_("UUID"), default=uuid.uuid4)
oto = models.OneToOneField(AA, on_delete=models.CASCADE, null=True, blank=True)
fk = models.ForeignKey(BB, on_delete=models.CASCADE, related_name="xxs", null=True, blank=True)
mtm = models.ManyToManyField(CC, blank=True)
created = models.DateTimeField(_("DateTime Created"), auto_now_add=True, help_text=_("When this item was created"))
modified = models.DateTimeField(_("DateTime Modified"), auto_now=True, help_text=_("When this item last updated"))
integer_range_field = IntegerRangeField(_("IntegerRange"))
big_integer_range_field = BigIntegerRangeField(_("BigIntegerRange"))
decimal_range_field = DecimalRangeField(_("DecimalRange"))
date_time_range_field = DateTimeRangeField(_("DateTimeRange"))
date_range_field = DateRangeField(_("DateRange"))
point = models.PointField(srid=4326)
multi_point = models.MultiPointField()
polygon = models.PolygonField()
multi_polygon = models.MultiPolygonField()
linestring = models.LineStringField()
multi_linestring = models.MultiLineStringField()
geometry_collection = models.GeometryCollectionField()
raster = models.RasterField()
critical = models.BooleanField(_("Critical"), default=False)
expired = models.DateTimeField(_("Expired"), null=True, blank=True)
CombinedMyModelManager = MyModelManager.from_queryset(MyModelQuerySet)
objects = CombinedMyModelManager()
unscoped = models.Manager()
class Meta:
verbose_name = _("My Model")
verbose_name_plural = _("My Models")
db_table = 'my_model'
get_latest_by = ['-datetime_field']
ordering = ['-datetime_field']
unique_together = [
['char_field', 'slug_field']
]
index_together = [
["created", "char_field"],
]
indexes = [
models.Index(fields=['char_field', 'text_field']),
models.Index(fields=['url_field'], name='url_field_idx'),
]
constraints = [
models.CheckConstraint(check=models.Q(integer_field__gte=18), name='integer_gte_18'),
models.UniqueConstraint(fields=['created', 'slug_field'], name='unique_slug_created'),
models.UniqueConstraint(
fields=['fk'],
condition=Q(char_field_choice=CompanyType.LIMITED_LIABILITY_PARTNERSHIP),
name='unique_llc_for_fk'
),
ExclusionConstraint(
name='exclude_overlapping_reservations',
expressions=[
('integer_range_field', RangeOperators.OVERLAPS),
('fk', RangeOperators.EQUAL),
],
condition=Q(expired__isnull=True),
),
]
def __str__(self):
return f'{self.char_field} {self.char_field}'
def save(self, *args, **kwargs):
if not self.pk:
self.slug_field = create_rand_value(
app_label=self._meta.app_label,
model_name=self.__class__.__name__,
prefix="",
suffix_len=10
)
super().save(*args, **kwargs)
def get_absolute_url(self):
from django.urls import reverse
return reverse('people.views.details', args=[str(self.id)])
@property
def char_slug(self):
return self.char_field.lower().replace(' ','-')
def expire_item(self):
self.expired = timezone.now()
self.save()
Example usage of the QuerySet, Manager, and Model methods:
Item.objects.all()
Item.objects.expired_items()
item = Item.objects.first()
item.expire_item()
Item.objects.delete_expired_items()
Have any additional tips/recommendations? Share them. I love to refine this over time.