Skip to main content

Django Utils is a module with some convenient utilities not included with the standard Django install

Project description

Travis status:

https://travis-ci.org/WoLpH/django-utils.svg?branch=master

Coverage:

https://coveralls.io/repos/WoLpH/django-utils/badge.svg?branch=master

Django Utils is a collection of small Django helper functions, utilities and classes which make common patterns shorter and easier. It is by no means a complete collection but it has served me quite a bit in the past and I will keep extending it.

Examples are:

  • Admin Select (Dropdown) filters

  • Admin Select2 (Autocomplete dropdown) filters

  • Admin JSON sub-field filters

  • Enum based choicefields

  • Models with automatic __str__, __unicode__ and __repr__ functions based on names and/or slugs using simple mixins.

  • Models with automatic updated_at and created_at fields

  • Models with automatic slugs based on the name property.

  • Iterating through querysets in predefined chunks to prevent out of memory errors

The library depends on the Python Utils library.

Documentation is available at: http://django-utils-2.readthedocs.io/en/latest/

Install

To install:

  1. Run pip install django-utils2 or execute python setup.py install in the source directory

  2. Add django_utils to your INSTALLED_APPS

If you want to run the tests, run py.test (requirements in tests/requirements.txt)

Admin Select / Dropdown / Autocomplete (JSON) Filters

All of the standard admin list filters are available through django_utils .admin.filters as:

  • The original filter (e.g. SimpleListFilter)

  • A basic select/dropdown filter: SimpleListFilterDropdown

  • A select2 based autocompleting dropdown filter: SimpleListFilterSelect2

On PostgreSQL you can additionally filter on JSON fields as well given paths:

class SomeModelAdmin(admin.ModelAdmin):
    list_filter = (
        JSONFieldFilterSelect2.create('some_json_field__some__sub_path'),
    )

That will filter a JSON field named some_json_field and look for values like this:

{"some": {"sub_path": "some value"}}

By default the results for the JSON filters are cached for 10 minutes but can be changed through the create parameters.

Choices usage

To enable easy to use choices which are more convenient than the Django 3.0 choices system you can use this:

from django_utils import choices


# For manually specifying the value (automatically detects ``str``, ``int`` and ``float``):
class Human(models.Model):
    class Gender(choices.Choices):
        MALE = 'm'
        FEMALE = 'f'
        OTHER = 'o'

    gender = models.CharField(max_length=1, choices=Gender)


# To define the values as ``male`` implicitly:
class Human(models.Model):
    class Gender(choices.Choices):
        MALE = choices.Choice()
        FEMALE = choices.Choice()
        OTHER = choices.Choice()

    gender = models.CharField(max_length=1, choices=Gender)


# Or explicitly define them
class Human(models.Model):
    class Gender(choices.Choices):
        MALE = choices.Choice('m', 'male')
        FEMALE = choices.Choice('f', 'female')
        OTHER = choices.Choice('o', 'other')

    gender = models.CharField(max_length=1, choices=Gender)

A PostgreSQL ENUM field will be coming soon to automatically facilitate the creation of the enum if needed.

Project details


Download files

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

Source Distribution

django-utils2-2.11.0.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

django_utils2-2.11.0-py2.py3-none-any.whl (19.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-utils2-2.11.0.tar.gz.

File metadata

  • Download URL: django-utils2-2.11.0.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for django-utils2-2.11.0.tar.gz
Algorithm Hash digest
SHA256 d957f669a23801d0a65c99502bb1da0a0cfc13dcc71a85bd55a80aa665c13519
MD5 a129dc60d944e8961c1ab1a13611b01e
BLAKE2b-256 128f5c5d63b4bc5d6a48c4a2abbf935d59848f6a682fa4834a910f7024bb807e

See more details on using hashes here.

File details

Details for the file django_utils2-2.11.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_utils2-2.11.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 87e9b0c9c639d79ac8ba2b051f9fd017b36c811decaa41c7f4e47a3652b85613
MD5 b887f2718cf6fedddd0a8f7bb88a99fa
BLAKE2b-256 3dd6e51cc153870aad38e2f861fcb48774f944568888e03e4c5c555fb79b1e88

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page