Skip to main content

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

  1. 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
]
  1. 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.py that 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 SystemEnumRegistry so 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 RetrieveEnumAPIView that returns a full representation via get_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

dj_standard_enums-0.1.2.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_standard_enums-0.1.2-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file dj_standard_enums-0.1.2.tar.gz.

File metadata

  • Download URL: dj_standard_enums-0.1.2.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for dj_standard_enums-0.1.2.tar.gz
Algorithm Hash digest
SHA256 65a3104768e684b223b11759e623b8d84cb1fa6205e290ef38526e515a86caed
MD5 205599c3e47e15893d6a2336aef28c2d
BLAKE2b-256 48e169ba6340864d9c5b87a8245fdb562a488d9ce6a98c276e2bc68fe490f24f

See more details on using hashes here.

File details

Details for the file dj_standard_enums-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for dj_standard_enums-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 98a20c322d1b4240511acd76cf6d9bf02bef44bf5d0afaacd9a58a0a3b6502d5
MD5 9de8e09d9ebd021189fd421bb0f4a6ab
BLAKE2b-256 e076e89b754307718bc1b9c26cffd872fd2afca1280a3486c0a96bfbd0f8ee5a

See more details on using hashes here.

Supported by

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