django_custom_jsonfield
An extended JSON field for Django and Django REST framework with validation support using jsonschema.
Usage
Installation
To install the minimal version of the package, run:
pip install django_custom_jsonfield
Defining a model field
Import CustomJSONField and define your schema. Here’s an example of how to use it in a model:
from django.db import models
from django_custom_jsonfield import CustomJSONField
class Location(models.Model):
coordinates = CustomJSONField(
schema={
"type": "object",
"properties": {
"x": {"type": "number"},
"y": {"type": "number"},
},
"required": ["x", "y"],
},
)
Location(coordinates={"x": 45, "y": 45}) # ok
Location(coordinates={"x": 45, "z": 45}) # ValidationError
You can customize the error message, if the value didn't pass JSON schema validation:
class Location(models.Model):
coordinates = CustomJSONField(
schema={...},
error_messages={"invalid_data": "Expected x and y keys."},
)
DRF Serializers
To enable DRF support, install package with extras:
pip install 'django_custom_jsonfield[drf]'
You can now use CustomJSONField in DRF serializers:
from rest_framework import serializers
from django_custom_jsonfield.rest_framework.serializers import CustomJSONField
class LocationSerializer(serializers.Serializer):
address = CustomJSONField(schema={"type": "string"})
To specify custom error message use the same positional argument as you use in models:
class LocationSerializer(serializers.Serializer):
address = CustomJSONField(
schema={"type": "string"},
error_messages={"invalid_data": "Expected type `string`."},
)
OpenAPI Integration
This package includes extension for drf-spectacular, allowing your API documentation
to correctly display the expected JSON schema. To access this feature, install the package with the [drf] extra.
Migrating existing data
The CustomJSONField does not impose any constraints on existing data.
Therefore, you can change a field from default JSONField to CustomJSONField even if
some rows violate the schema. However, it is recommended to follow these steps to
ensure a smooth transition:
- Create a new field: add a new field of type
CustomJSONFieldto your model. - Data migration: Perform a data migration to copy the values from the old field to the new field, ensuring the data conforms to the schema.
- Replace the old field: Remove the old field and rename the new field to match the old field's name.
Following these steps will ensure that your data complies with the new schema.
Metadata
Release files for django-custom-jsonfield 1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_custom_jsonfield-1.3.tar.gz | 8.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_custom_jsonfield-1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.7 kB
Release files / django_custom_jsonfield-1.3.tar.gz
| Download URL | django_custom_jsonfield-1.3.tar.gz |
|---|---|
| Size | 8.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1e21b298b728a2c3a470e26a502112bcb81c5076e145d9d92ca5be74fb2cc34c
|
|
BLAKE2b-256 checksum How to use checksums |
e081bf461d76e5f80a825eea96a40a60b75d14080949c12f1dbc53183995542f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / django_custom_jsonfield-1.3-py3-none-any.whl
| Download URL | django_custom_jsonfield-1.3-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
750893aa83b6f7ef6fa9cb8a11c01f4c4f90a5adfdc941a74443aeb7ed2593d9
|
|
BLAKE2b-256 checksum How to use checksums |
5946486f3de8d0236229a04427fb60bc517d2bc491bb4792e487d91e57d53821
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|