Skip to main content

Schema-validated JSONFields for Django using an EAV-style DSL

Project description

django-eav-json-fields

Schema-validated JSONFields for Django using an EAV-style DSL.

Define expected keys, types, and constraints on JSON dict values stored in Django JSONFields. Storage remains plain JSONB in PostgreSQL -- the EAV layer adds validation, type coercion, and admin widgets.

Requirements

  • Python 3.10+
  • Django 4.2+

Installation

pip install django-eav-json-fields

No INSTALLED_APPS entry is needed.

Quick Start

Define a schema

from eav_fields import EAVSchema, EAVBoolean, EAVString, EAVDecimal, EAVInteger

class ServiceConfig(EAVSchema):
    enabled = EAVBoolean(default=True, help_text="Enable this service")
    label = EAVString(max_length=100, help_text="Display name")
    price = EAVDecimal(required=False, max_digits=10, decimal_places=2)
    max_retries = EAVInteger(min_value=0, max_value=10, default=3, required=False)

Use EAVField on a model

from django.db import models
from eav_fields import EAVField

class Service(models.Model):
    name = models.CharField(max_length=100)
    config = EAVField(schema=ServiceConfig)

The field validates during full_clean() that the JSON dict conforms to the schema. Unknown keys are rejected, types are coerced, and constraints are enforced.

Polymorphic schemas

Select a schema based on another field's value:

class Product(models.Model):
    product_type = models.CharField(max_length=20, choices=[("a", "Type A"), ("b", "Type B")])
    config = EAVField(
        schema_map={"a": SchemaA, "b": SchemaB},
        schema_key_field="product_type",
    )

EAV Attribute Types

Type Python Type JSON Storage Constraints
EAVString str string max_length, choices
EAVBoolean bool boolean --
EAVInteger int number min_value, max_value, choices
EAVFloat float number min_value, max_value, choices
EAVDecimal Decimal string max_digits, decimal_places, min_value, max_value, choices

All attributes support: required (default True), default, help_text, choices.

Cross-field Validation

Override validate_cross for rules spanning multiple attributes:

from django.core.exceptions import ValidationError
from eav_fields import EAVSchema, EAVInteger

class MyConfig(EAVSchema):
    min_val = EAVInteger()
    max_val = EAVInteger()

    @classmethod
    def validate_cross(cls, data):
        if data.get("min_val", 0) >= data.get("max_val", 0):
            raise ValidationError({"max_val": ["max_val must be greater than min_val."]})

Schema Inheritance

Child schemas inherit parent attributes and can override them:

class BaseConfig(EAVSchema):
    name = EAVString()

class ExtendedConfig(BaseConfig):
    count = EAVInteger()  # inherits 'name' from parent

Admin Widget

EAVField automatically provides an EAVWidget in the Django admin that renders typed HTML inputs for each schema attribute, with show/hide support for polymorphic schemas.

Migration Support

Schema references are serialized as dotted import paths in migrations. Both class references and string paths are accepted:

# Both are equivalent:
config = EAVField(schema=MySchema)
config = EAVField(schema="myapp.schemas.MySchema")

License

MIT

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_eav_json_fields-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

django_eav_json_fields-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file django_eav_json_fields-0.1.0.tar.gz.

File metadata

  • Download URL: django_eav_json_fields-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_eav_json_fields-0.1.0.tar.gz
Algorithm Hash digest
SHA256 74a60dab87805fd5e87bb98808f2b9c4f47042e87c860d1d624b0a69a8dc2afb
MD5 bef48d8904f26678d44c39947a7161d8
BLAKE2b-256 1a4cbce1088b1f834774e286b87af86064db1a1a187620cdeb318cbcf7ba738e

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_eav_json_fields-0.1.0.tar.gz:

Publisher: publish.yml on kolanos/django-eav-json-fields

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_eav_json_fields-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_eav_json_fields-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f3a6381c66b40a5480e449e774b5cdb8246fe82445bb3437fcc26afecbaacf1
MD5 707d38bb42eb5778afdb2f2f77e45f6b
BLAKE2b-256 387badabcdcbe87ac632c29a3381718835602c04416fb682a693620a1ffa24bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_eav_json_fields-0.1.0-py3-none-any.whl:

Publisher: publish.yml on kolanos/django-eav-json-fields

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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