Skip to main content

A collection of utilities that make dealing with Django models more fun.

Project description


  1. pip install django-modeltools

Included Tools


The Enum class lets you quickly define enumeration types for model field values. The constructor accepts kwargs where the key is the name of the “constant” and the value is a two-tuple containing the value of the constant (which will be stored in the database) and a label.



from modeltools import Enum

class MyModel(models.Model):

    Color = Enum(
        RED=('r', 'Red'),
        GREEN=('g', 'Green'),
        BLUE=('b', 'Blue'),

    color = models.CharField(max_length=1, choices=Color.choices())


m = MyModel.objects.filter(color=MyModel.Color.RED)


The format_filename function provides an easy way to name user media (uploaded files) based on properties of the model that stores them.



from modeltools import format_filename as _ff

class Person(models.Model):
        first_name = models.CharField(max_length=50)
        last_name = models.CharField(max_length=50)
        middle_name = models.CharField(max_length=50)
        avatar = models.ImageField(upload_to=_ff('avatars/{last_name}_{first_name}'))

In the above example, {first_name} and {last_name} will be replaced with the corresponding values from the Person instance. The uploaded file will automatically retain its original extension.


{__filename} and {__ext} allows access to the name and extension the file was uploaded with.

By default, the properties used in the formatting pattern will be converted to lowercase, stripped of non-word characters, and have their spaces replaced with underscores. (This behavior can be changed by providing extra arguments to the format_filename function.) The rest of the formatting string will be unaffected.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-modeltools, version 1.0.1
Filename, size File type Python version Upload date Hashes
Filename, size django-modeltools-1.0.1.tar.gz (5.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page