Skip to main content

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

  1. Add 'django_pg_jsonschema' to your Django project's INSTALLED_APPS in the settings module.

  2. Configure globally if you want to commit the schemas to PostgreSQL (in settings.py):

    DJANGO_PG_JSONSCHEMA = {
        'PG_COMMIT_JSONSCHEMA': True
    }
    
  3. Import the JSONSchemaField from the package:

    from django_pg_jsonschema.fields import JSONSchemaField
    
  4. 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.

  5. 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
         )
    
  6. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_pg_jsonschema-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

django_pg_jsonschema-0.1.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file django_pg_jsonschema-0.1.1.tar.gz.

File metadata

  • Download URL: django_pg_jsonschema-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.9

File hashes

Hashes for django_pg_jsonschema-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ab69acf0f65c087cac97f675ddfad30405540f5143dd4a9d1518ed5dbcc24069
MD5 933d0d6a11a09c482fbaa49f0c088975
BLAKE2b-256 2b9eb910a87baea60b4d1d7d77faf63ef678070969cf47507eec5eb3313639f0

See more details on using hashes here.

File details

Details for the file django_pg_jsonschema-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_pg_jsonschema-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 16f4b46e8b433d2b7dea7667fecd926c8afc161bff1543d0ddbce518d7ead708
MD5 dc29ce942f737b47b2af16fcd4f51547
BLAKE2b-256 7cef6ed56d705eb11cf8f1740fcbcdaac644ac8647b67c033190f3e8f19e3337

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