A standard way to use enums in Django and expose them via API so that they can be used in the Front End thereby reducing the code duplication.
Project description
dj_standard_enums
A standard way to define, register, and expose enums in Django. This package provides:
- A simple, consistent pattern to define enums once in your Django backend.
- A registry to look them up by name.
- A REST API endpoint to expose enum values to your Front End (React, Vue, etc.), reducing duplication of constants between BE and FE.
In short, this package aims to establish a standard way to use enums in Django and expose them via API so that they can be used in the Front End, thereby reducing code duplication.
Installation
This package is published as dj_standard_enums on PyPI.
- One-time install with pip:
pip install dj_standard_enums
- Pin it in your project's requirements.txt:
dj_standard_enums>=0.0.2
The package depends on Django and Django REST Framework, which will be installed automatically if not already present.
Quickstart
- Add the app to INSTALLED_APPS
In your Django settings (e.g., settings.py):
INSTALLED_APPS = [
# ... your apps ...
"rest_framework", # if not already included
"enums", # provided by dj_standard_enums
]
- Include the enums URLs
In your project-level urls.py:
from django.urls import include, re_path
urlpatterns = [
# ... other urls ...
re_path(r"", include("enums.urls")),
]
This will make the enums API available at the route defined by the package, for example:
- GET
/api/v1/enums/<EnumClassName>
Where <EnumClassName> is the name used to register your enum in the registry.
How it works
- The package defines an API view and URL pattern in
enums/urls.pythat serves enums by class name. - The endpoint:
^api/v1/enums/(?P<enum_class_name>[0-9a-zA-Z\-]+)$maps to a view that queries the internal registry and returns the enum representation as JSON. - You can then fetch enums in your Front End and avoid duplicating constant lists.
Defining and exposing your enums
At a high level, you define enums using the package's framework and register them so they can be retrieved by the API. A typical flow is:
- Create an enum subclass using the provided base classes in
enums.framework. - Ensure your enum is registered with the
SystemEnumRegistryso it can be fetched by name. - In order to register your enum, add your Enum class's dotted path in your django settings.py file in the STANDARD_ENUMS_REGISTRY setting.
- Access it in FE via the API endpoint:
/api/v1/enums/<EnumClassName>.
Example response shape (simplified):
[
{"value": "ACTIVE", "label": "Active"},
{"value": "INACTIVE", "label": "Inactive"}
]
Notes:
- The package ships a
RetrieveEnumAPIViewthat returns a full representation viaget_full_representation()defined on your enum classes. - Authentication is disabled by default on this view; you can wrap it with your own project-level auth or gate it via URL configuration if needed.
Why use this package?
- Single source of truth for enums shared across BE/FE.
- Standardized registry and retrieval pattern.
- Fewer bugs and less drift between backend choices and frontend constants.
Requirements
- Python 3.8+
- Django 3.2+ (tested up to Django 5.x)
- Django REST Framework
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dj_standard_enums-0.1.3.tar.gz.
File metadata
- Download URL: dj_standard_enums-0.1.3.tar.gz
- Upload date:
- Size: 17.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d10796338fb07beeeb1c56fe73c19b32698fd3daf72996090e56754488a90d0b
|
|
| MD5 |
3aa05a2b130ec795b08009b958556523
|
|
| BLAKE2b-256 |
21c9fd3c6ebfe9053ade45c75db84965b3e84242e63da9fedb9ae520fe2f9e61
|
File details
Details for the file dj_standard_enums-0.1.3-py3-none-any.whl.
File metadata
- Download URL: dj_standard_enums-0.1.3-py3-none-any.whl
- Upload date:
- Size: 24.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9203b10b76cba8f9b7396f77c3819f7b31c89729ba0e9ef754eeac92c6e07dbb
|
|
| MD5 |
b6a766b67c6ff43e92e3a5fa9e9234a2
|
|
| BLAKE2b-256 |
453b3c876156200e2148258a3b0fae133e3500474327071fbd6cf6bd57b45fcc
|