Skip to main content

Django ChoiceField

CI Build Status PyPI Package Python versions

Motivation

Have you also felt annoyed by having to convert Django's enumeration types back to its type? Using tricks seen below to cast it.

class Suit(models.IntegerChoices):
    DIAMOND = 1
    SPADE = 2
    HEART = 3
    CLUB = 4


class Card(models.Model):
    suit_kind = models.IntegerField(choices=Suit.choices, db_column="suit")

    @property
    def suit(self) -> Suit:
        return Suit(self.suit_kind)

This is what django-choicefield helps out with. While it additionally supports using Python's native enum.Enum to express column values.

Features

Using Django's enumeration types

import choicefield
from django.db import models


class Suit(models.IntegerChoices):
    DIAMOND = 1
    SPADE = 2
    HEART = 3
    CLUB = 4


class Card(models.Model):
    suit = choicefield.ChoiceField(Suit)


instance = Card.objects.create(suit=Suit.CLUB)
assert instance.suit is Suit.CLUB

There's also support for Django's models.TextChoices.

Using Python's native enumeration

import choicefield
from enum import Enum
from django.db import models


class Suit(int, Enum):
    DIAMOND = 1
    SPADE = 2
    HEART = 3
    CLUB = 4


class Card(models.Model):
    suit = choicefield.ChoiceField(Suit)


instance = Card.objects.create(suit=Suit.DIAMOND)
assert instance.suit is Suit.DIAMOND

Passing enum values

It's also possible to pass the value of an enum, which will be converted to its corresponding enum instance.

instance = Card(suit=2)
assert instance.suit is Suit.SPADE
instance.save()
assert instance.suit is Suit.SPADE
instance = Card.objects.get(suit=2)
assert instance.suit is Suit.SPADE

Getting stored database values

If you want to access the stored database values, without conversion to your enum type, you can use the registered __raw transformer.

Card.objects.values("suit__raw")
# <QuerySet [{'suit__raw': 2}]>

Getting unrecognized values from database

In case of e.g. a migration where an enum has changed by, say, removing a value. The database could have values not recognized by the registered enum. Thus it could be necessary to retrieve values without casting them to an enum instance, as it'd raise an error.

It can be done using the __raw transformer while also sidestepping enum validation in filter values by using Value expressions

Card.objects.filter(suit=Value(1337)).values_list("suit__raw", flat=True)
# <QuerySet [(1337,)]>

Installation

Using pip

$ pip install django-choicefield

Development

Running tests

Running the whole test matrix

$ tox

Setting up a development environment

$ tox -e dev

Running the test suite for one environment (non editable)

e.g. Django==4.0.x and Python3.11

$ tox -e django40-py311

Start a local example project

There are a couple of shortcut commands available using Taskfile, for your convenience.

e.g.

$ task manage -- createsuperuser
$ task runserver

After installing Taskfile you can run task --list-all to find all available commands.

Compatibility

django-choicefield is tested according to the table below

Django version Python version
5.0.x ^3.10
4.2.x ^3.9
4.1.x ^3.9
4.0.x ^3.9
3.2.x ^3.9

Release files for django-choicefield 0.2.3

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-choicefield 0.2.3
File Size Uploaded
django_choicefield-0.2.3.tar.gz 16.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-choicefield 0.2.3
File Interpreter ABI Platform
django_choicefield-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 23.2 kB

Release files / django_choicefield-0.2.3.tar.gz

Download URL django_choicefield-0.2.3.tar.gz
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a5f570065c8f5ff50bdac767744f5bd8bcdc156f184df5ef3e062da304aa7eda
BLAKE2b-256 checksum
How to use checksums
c5e6ffbe615067396790c1bf4de78ca7651eb6b8ddb542312928821a03c0c988
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / django_choicefield-0.2.3-py3-none-any.whl

Download URL django_choicefield-0.2.3-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b7eeafdc9480819fa0c2cfabae58e395026712b32ebdf01f8a2db578bc3ba01
BLAKE2b-256 checksum
How to use checksums
edcc830cadc12b9d9fd941ef41bcb4e93859533d4ef0e0b89130da670865c061
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

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