Skip to main content

A Django JSONField extension using Pydantic for data validation.

Project description

Django Pydantic JSONField

Description

Django Pydantic JSONField is a Django app that extends Django's native JSONField by integrating Pydantic models for data validation. This package allows developers to leverage Pydantic's powerful data validation and schema enforcement capabilities within their Django models, ensuring that data stored in JSONFields is validated against predefined Pydantic models.

Features

  • Seamless integration of Pydantic models with Django models.
  • Customizable JSON serialization options via extendable encoders.
  • Support for complex data types and validation provided by Pydantic.

Installation

To install Django Pydantic JSONField, run the following command in your virtual environment:

pip install django-pydantic-jsonfield

Quick Start

Defining Your Pydantic Model

from pydantic import BaseModel
from datetime import datetime

class ProductDetails(BaseModel):
    name: str
    description: str
    price: float
    tags: list[str]
    created: datetime

Using PydanticJSONField in Your Django Model

Every PydanticJSONField requires a pydantic_model argument, which is the Pydantic model that will be used to validate the JSON data.

from django.db import models
from django_pydantic_jsonfield.fields import PydanticJSONField

class Product(models.Model):
    details = PydanticJSONField(
        pydantic_model=ProductDetails,
    )

Interacting with Your Model

from datetime import datetime

product = Product(details={
    "name": "Smart Watch",
    "description": "A smart watch with health tracking features.",
    "price": 199.99,
    "tags": ["wearable", "health", "gadget"],
    "created": datetime.now(),
})
product.save()

# Accessing the details field will return a Pydantic model instance
print(product.details.name)
product.details.description = "My new description"
product.save()

Advanced Usage

Creating a Custom Encoder

Pydantic objects are serialized using the Pydantic model_dump_json method which can handle complex data types. If you'd like to take advantage of some of the serialization options available on that method, you can use a CustomEncoder to pass in additional arguments.

class CustomPydanticModelEncoder(PydanticModelEncoder):
    default_model_dump_json_options = {
        "indent": 2,
        "exclude_none": True,
    }

class Product(models.Model):
    details = PydanticJSONField(
        pydantic_model=ProductDetails,
        encoder=CustomPydanticModelEncoder,
    )

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-pydantic-jsonfield-0.1.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

django_pydantic_jsonfield-0.1.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file django-pydantic-jsonfield-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django-pydantic-jsonfield-0.1.2.tar.gz
Algorithm Hash digest
SHA256 90ab8634a5749f3db6eb09578ed50173d212f9382d67766269d20f9f928524b5
MD5 ccffb694540a51e00ff5948508af1b3d
BLAKE2b-256 8a057a82d1d3a34c4213f3f2e3b6d451b25759e1163502e9fdf125bb66aa6753

See more details on using hashes here.

File details

Details for the file django_pydantic_jsonfield-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_pydantic_jsonfield-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6553a1b865e141baccd07dc0c83e790a2890619cbfa2fd563fb7880bbc475bf2
MD5 649dc90106bc9119ddf6fcdc9edd39a5
BLAKE2b-256 a7e426ce11474dfb0a35345d7c45553dde915a280fa8c7a933bdf47097265290

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