Skip to main content

A Django enum field

Project description

django_state_choice_field

Checks & Tests

It includes a StateChoiceField that comes with reusable TextChoices and validation for state changes. This feature enhances flexibility and data integrity.

Requirements

  • Python 3.6+
  • Django 2.2+

Installation

To set up django_state_choice_field, you can easily install it with pip.

$ pip install django_state_choice_field

Example

Consider a scenario where we have a model called Order that includes the storage of the order's payment status. This payment status can fall into one of the following in PaymentStatus.

It also defines the state transitions that are allowed for each state. For example, a payment status of IN_PROGRESS can only be changed to FAILED or COMPLETED. This is done by defining the __states__ attribute in the PaymentStatus class which extends StateEnum.

from django_state_choice_field import StateChoiceField, StateEnum

class PaymentStatus(StateEnum):
    NOT_STARTED = "not_started", "Not Started"
    IN_PROGRESS = "in_progress", "In Progress"
    COMPLETED = "completed", "Completed"
    FAILED = "failed", "Failed"
    CANCELLED = "cancelled", "Cancelled"
    NOT_REQUIRED = "not_required", "Not Required"

    __states__ = {
        NOT_STARTED: (),
        IN_PROGRESS: (NOT_STARTED, FAILED),
        FAILED: (IN_PROGRESS,),
        COMPLETED: (IN_PROGRESS, NOT_REQUIRED),
        NOT_REQUIRED: (IN_PROGRESS,),
        CANCELLED: (NOT_STARTED, NOT_REQUIRED, FAILED, COMPLETED),
    }

Model Order can be defined as follows. The payment_status field is defined as a StateChoiceField with the PaymentStatus enum class.

from django.db import models

class Orders(models.Model):
    product_name = models.CharField(max_length=100)
    payment_status = StateChoiceField(
        PaymentStatus, default=PaymentStatus.NOT_STARTED, max_length=20
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Usage

>>> order = Order.objects.create(product_name="Product 1")
>>> order.payment_status
<PaymentStatus.NOT_STARTED: 'not_started'>

>>> order.payment_status = PaymentStatus.IN_PROGRESS
>>> order.save()

>>> order.payment_status
<PaymentStatus.IN_PROGRESS: 'in_progress'>

# Now, if we try to change the payment status to CANCELLED, it will raise a InvalidTransitionError error.
>>> order.payment_status = PaymentStatus.CANCELLED

django_state_choice_field.exceptions.InvalidTransitionError: [in_progress -> cancelled is not a valid transition for PaymentStatus']

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_state_choice_field-0.0.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django_state_choice_field-0.0.1.tar.gz.

File metadata

File hashes

Hashes for django_state_choice_field-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ff4e36c13a618403894fb0518509948c294d80da1938a3670ba7076952338328
MD5 786c9035911b24d61907688c3978824f
BLAKE2b-256 2ba1fc03dfa68de4ba90c4b0149da37f9f2c5ad585e4d2a0ed2ddb894f83697f

See more details on using hashes here.

File details

Details for the file django_state_choice_field-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_state_choice_field-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 daee1dae1636e20d603bf0a2de89fa08bd07ef88999a805f7dde9b840ba6b254
MD5 907c93ad1b0c083a386f1bab337bffcf
BLAKE2b-256 9d958613da1219aab156e54a3ef5bd52d27989cca0c1d799a049b0d104b0ca8a

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