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
Release history Release notifications | RSS feed
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74a60dab87805fd5e87bb98808f2b9c4f47042e87c860d1d624b0a69a8dc2afb
|
|
| MD5 |
bef48d8904f26678d44c39947a7161d8
|
|
| BLAKE2b-256 |
1a4cbce1088b1f834774e286b87af86064db1a1a187620cdeb318cbcf7ba738e
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_eav_json_fields-0.1.0.tar.gz -
Subject digest:
74a60dab87805fd5e87bb98808f2b9c4f47042e87c860d1d624b0a69a8dc2afb - Sigstore transparency entry: 975315764
- Sigstore integration time:
-
Permalink:
kolanos/django-eav-json-fields@30c91a4c96f077e0bddc43b64a929c67a9ea72a7 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/kolanos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@30c91a4c96f077e0bddc43b64a929c67a9ea72a7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_eav_json_fields-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_eav_json_fields-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f3a6381c66b40a5480e449e774b5cdb8246fe82445bb3437fcc26afecbaacf1
|
|
| MD5 |
707d38bb42eb5778afdb2f2f77e45f6b
|
|
| BLAKE2b-256 |
387badabcdcbe87ac632c29a3381718835602c04416fb682a693620a1ffa24bd
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_eav_json_fields-0.1.0-py3-none-any.whl -
Subject digest:
5f3a6381c66b40a5480e449e774b5cdb8246fe82445bb3437fcc26afecbaacf1 - Sigstore transparency entry: 975315765
- Sigstore integration time:
-
Permalink:
kolanos/django-eav-json-fields@30c91a4c96f077e0bddc43b64a929c67a9ea72a7 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/kolanos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@30c91a4c96f077e0bddc43b64a929c67a9ea72a7 -
Trigger Event:
release
-
Statement type: