Skip to main content

Installation

  1. pip install django-modeltools

Included Tools

Enum

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.

Usage

In models.py:

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

Elsewhere:

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

format_filename

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

Usage

In models.py:

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.

upload_to=_ff('avatars/{last_name}_{first_name}/{__filename}.thumbnail{__ext}')

{__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.

Release files for django-modeltools 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-modeltools 1.1.1
File Size Uploaded
django-modeltools-1.1.1.tar.gz 5.5 kB Details

Release files / django-modeltools-1.1.1.tar.gz

Download URL django-modeltools-1.1.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
259e1fbeccb8311e302f279e512b503fbd64c96c92bbc5700cd5f0ef1e23f009
BLAKE2b-256 checksum
How to use checksums
16d5d5fb09f93aa07ccef3c206d89c0ca2a9ab14238653e677ba7b6160a6ff89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.8

Release history Release notifications | RSS feed

This release

1.1.1 This release

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page