django_state_choice_field
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']
Metadata
Release files for django-state-choice-field 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_state_choice_field-0.0.1.tar.gz | 6.9 kB | Without provenance | Details for django_state_choice_field-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_state_choice_field-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for django_state_choice_field-0.0.1-py3-none-any.whl |
Total release size: 14.8 kB
Release files / django_state_choice_field-0.0.1.tar.gz
| Download URL | django_state_choice_field-0.0.1.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ff4e36c13a618403894fb0518509948c294d80da1938a3670ba7076952338328
|
|
BLAKE2b-256 checksum How to use checksums |
2ba1fc03dfa68de4ba90c4b0149da37f9f2c5ad585e4d2a0ed2ddb894f83697f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / django_state_choice_field-0.0.1-py3-none-any.whl
| Download URL | django_state_choice_field-0.0.1-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
daee1dae1636e20d603bf0a2de89fa08bd07ef88999a805f7dde9b840ba6b254
|
|
BLAKE2b-256 checksum How to use checksums |
9d958613da1219aab156e54a3ef5bd52d27989cca0c1d799a049b0d104b0ca8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for django-state-choice-field 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.