django-pydantic-jsonfield


Namedjango-pydantic-jsonfield JSON
Version 0.1.2 PyPI version JSON
download
home_page
SummaryA Django JSONField extension using Pydantic for data validation.
upload_time2024-03-03 04:45:15
maintainer
docs_urlNone
author
requires_python
licenseMIT License Copyright (c) 2024 Brian Guggenheimer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords django pydantic jsonfield
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

```bash
pip install django-pydantic-jsonfield
```

## Quick Start

### Defining Your Pydantic Model

```python
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.

```python
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

```python
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](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.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.

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

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



            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "django-pydantic-jsonfield",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "django,pydantic,jsonfield",
    "author": "",
    "author_email": "Brian Guggenheimer <bguggs@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/8a/05/7a82d1d3a34c4213f3f2e3b6d451b25759e1163502e9fdf125bb66aa6753/django-pydantic-jsonfield-0.1.2.tar.gz",
    "platform": null,
    "description": "# Django Pydantic JSONField\n\n## Description\n\nDjango 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.\n\n## Features\n\n- Seamless integration of Pydantic models with Django models.\n- Customizable JSON serialization options via extendable encoders.\n- Support for complex data types and validation provided by Pydantic.\n\n## Installation\n\nTo install Django Pydantic JSONField, run the following command in your virtual environment:\n\n```bash\npip install django-pydantic-jsonfield\n```\n\n## Quick Start\n\n### Defining Your Pydantic Model\n\n```python\nfrom pydantic import BaseModel\nfrom datetime import datetime\n\nclass ProductDetails(BaseModel):\n    name: str\n    description: str\n    price: float\n    tags: list[str]\n    created: datetime\n```\n\n### Using PydanticJSONField in Your Django Model\n\nEvery `PydanticJSONField` requires a `pydantic_model` argument, which is the Pydantic model that will be used to validate the JSON data.\n\n```python\nfrom django.db import models\nfrom django_pydantic_jsonfield.fields import PydanticJSONField\n\nclass Product(models.Model):\n    details = PydanticJSONField(\n        pydantic_model=ProductDetails,\n    )\n```\n\n### Interacting with Your Model\n\n```python\nfrom datetime import datetime\n\nproduct = Product(details={\n    \"name\": \"Smart Watch\",\n    \"description\": \"A smart watch with health tracking features.\",\n    \"price\": 199.99,\n    \"tags\": [\"wearable\", \"health\", \"gadget\"],\n    \"created\": datetime.now(),\n})\nproduct.save()\n\n# Accessing the details field will return a Pydantic model instance\nprint(product.details.name)\nproduct.details.description = \"My new description\"\nproduct.save()\n```\n\n## Advanced Usage\n\n### Creating a Custom Encoder\n\nPydantic objects are serialized using the Pydantic [model_dump_json](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.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.\n\n```python\nclass CustomPydanticModelEncoder(PydanticModelEncoder):\n    default_model_dump_json_options = {\n        \"indent\": 2,\n        \"exclude_none\": True,\n    }\n\nclass Product(models.Model):\n    details = PydanticJSONField(\n        pydantic_model=ProductDetails,\n        encoder=CustomPydanticModelEncoder,\n    )\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Brian Guggenheimer  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "A Django JSONField extension using Pydantic for data validation.",
    "version": "0.1.2",
    "project_urls": null,
    "split_keywords": [
        "django",
        "pydantic",
        "jsonfield"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a7e426ce11474dfb0a35345d7c45553dde915a280fa8c7a933bdf47097265290",
                "md5": "649dc90106bc9119ddf6fcdc9edd39a5",
                "sha256": "6553a1b865e141baccd07dc0c83e790a2890619cbfa2fd563fb7880bbc475bf2"
            },
            "downloads": -1,
            "filename": "django_pydantic_jsonfield-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "649dc90106bc9119ddf6fcdc9edd39a5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6192,
            "upload_time": "2024-03-03T04:45:13",
            "upload_time_iso_8601": "2024-03-03T04:45:13.325686Z",
            "url": "https://files.pythonhosted.org/packages/a7/e4/26ce11474dfb0a35345d7c45553dde915a280fa8c7a933bdf47097265290/django_pydantic_jsonfield-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8a057a82d1d3a34c4213f3f2e3b6d451b25759e1163502e9fdf125bb66aa6753",
                "md5": "ccffb694540a51e00ff5948508af1b3d",
                "sha256": "90ab8634a5749f3db6eb09578ed50173d212f9382d67766269d20f9f928524b5"
            },
            "downloads": -1,
            "filename": "django-pydantic-jsonfield-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "ccffb694540a51e00ff5948508af1b3d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5138,
            "upload_time": "2024-03-03T04:45:15",
            "upload_time_iso_8601": "2024-03-03T04:45:15.277516Z",
            "url": "https://files.pythonhosted.org/packages/8a/05/7a82d1d3a34c4213f3f2e3b6d451b25759e1163502e9fdf125bb66aa6753/django-pydantic-jsonfield-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-03 04:45:15",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "django-pydantic-jsonfield"
}
        
Elapsed time: 2.51165s