Django-implementation for PostgreSQL JSONSchema
Project description
Django PostgreSQL JSONSchema
Django PostgreSQL JSONSchema is a package that provides an extension to
Django's PostgreSQL JSONField, by adding support and functions for PostgreSQL
installations with pg_jsonschema
.
This allows developers to store data in a JSONField, while running validation over the field and having the ability to generate forms for these model fields.
Features
- Custom JSON Field: Extends Django's built-in
JSONField
to support JSON Schema validation. - PostgreSQL JSONSchema: Choose whether you want to validate the data in Python or commit the JSONSchema to the DB.
- Querying Support: Utilize Django's ORM capabilities to query and filter data stored in the JSON field.
- Schema Migration: Perform schema migrations smoothly without data loss or compatibility issues.
Installation
You can install the package using pip:
pip install django-pg-jsonschema
Usage
-
Add
'django_pg_jsonschema'
to your Django project'sINSTALLED_APPS
in the settings module. -
Configure globally if you want to commit the schemas to PostgreSQL (in
settings.py
):DJANGO_PG_JSONSCHEMA = { 'PG_COMMIT_JSONSCHEMA': True }
-
Import the
JSONSchemaField
from the package:from django_pg_jsonschema.fields import JSONSchemaField
-
In your Django model, define a field of type
JSONSchemaField
argument specifying the JSON schema:class Person(models.Model): data = JSONSchemaField(schema={ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer", "minimum": 0}, }, "required": ["name", "age"] })
In the above example, the
data
field will store JSON objects that adhere to the specified schema. -
Optionally, you can define whether you want the check to be added as a constraint to the database:
class Person(models.Model): data = JSONSchemaField( schema={ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer", "minimum": 0}, }, "required": ["name", "age"] }, check_schema_in_db=False )
-
Use the field in your Django models as you would with any other field:
obj = Person(data={"name": "John Doe", "age": 25}) obj.save()
Contribution
Contributions to the Django JSON Field with JSON Schema Support package are welcome! If you encounter any issues, have suggestions, or want to contribute code, please open an issue
or submit a pull request on the GitHub repository: https://github.com/maxboone/django-pg-jsonschema
Please make sure to follow the contribution guidelines before submitting your contributions.
License
This package is licensed under the Apache 2.0 License.
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
Hashes for django_pg_jsonschema-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0649da224c5609122e50c7a2aa1067763293303328ee84c7719cd3273e766cd8 |
|
MD5 | 303dc5cfef63c512a1913cb44fbe0eba |
|
BLAKE2b-256 | 2febcd50200bcac9c8591591b59f6f5e855855e6f9865b75e94992075b78c0a1 |
Hashes for django_pg_jsonschema-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8455348066cd5cba96ca66806790fd5d76a329e336fffb12590818a78928471b |
|
MD5 | ad7d77d2386618d01a5de4865a24a7dd |
|
BLAKE2b-256 | ec213d3914fa842a7623050bfc5a5135ee264d55f58a043b1a4ac700e4c08aed |