Giter Site home page Giter Site logo

djantic2's Introduction

Djantic2

Pydantic model support for Django

GitHub Workflow Status (Test) PyPi package Supported Python versions Supported Django versions


Djantic2 is a fork of djantic which works with pydantic >2, it is a library that provides a configurable utility class for automatically creating a Pydantic model instance for any Django model class. It is intended to support all of the underlying Pydantic model functionality such as JSON schema generation and introduces custom behaviour for exporting Django model instance data.

Quickstart

Install using pip:

pip install djantic2

Create a model schema:

from users.models import User
from pydantic import ConfigDict

from djantic import ModelSchema

class UserSchema(ModelSchema):
    model_config = ConfigDict(model=User, include=["id", "first_name"])

print(UserSchema.schema())

Output:

{
    "description": "A user of the application.",
    "properties": {
        "id": {
            "anyOf": [{"type": "integer"}, {"type": "null"}],
            "default": None,
            "description": "id",
            "title": "Id",
        },
        "first_name": {
            "description": "first_name",
            "maxLength": 50,
            "title": "First Name",
            "type": "string",
        },
    },
    "required": ["first_name"],
    "title": "UserSchema",
    "type": "object",
}

See https://pydantic-docs.helpmanual.io/usage/models/ for more.

Loading and exporting model instances

Use the from_django method on the model schema to load a Django model instance for export:

user = User.objects.create(
    first_name="Jordan",
    last_name="Eremieff",
    email="[email protected]"
)

user_schema = UserSchema.from_django(user)
print(user_schema.json(indent=2))

Output:

{
    "profile": null,
    "id": 1,
    "first_name": "Jordan",
    "last_name": "Eremieff",
    "email": "[email protected]",
    "created_at": "2020-08-15T16:50:30.606345+00:00",
    "updated_at": "2020-08-15T16:50:30.606452+00:00"
}

Using multiple level relations

Djantic supports multiple level relations. This includes foreign keys, many-to-many, and one-to-one relationships.

Consider the following example Django model and Djantic model schema definitions for a number of related database records:

# models.py
from django.db import models

class OrderUser(models.Model):
    email = models.EmailField(unique=True)


class OrderUserProfile(models.Model):
    address = models.CharField(max_length=255)
    user = models.OneToOneField(OrderUser, on_delete=models.CASCADE, related_name='profile')


class Order(models.Model):
    total_price = models.DecimalField(max_digits=8, decimal_places=5, default=0)
    user = models.ForeignKey(
        OrderUser, on_delete=models.CASCADE, related_name="orders"
    )


class OrderItem(models.Model):
    price = models.DecimalField(max_digits=8, decimal_places=5, default=0)
    quantity = models.IntegerField(default=0)
    order = models.ForeignKey(
        Order, on_delete=models.CASCADE, related_name="items"
    )


class OrderItemDetail(models.Model):
    name = models.CharField(max_length=30)
    order_item = models.ForeignKey(
        OrderItem, on_delete=models.CASCADE, related_name="details"
    )
# schemas.py
from djantic import ModelSchema
from pydantic import ConfigDict

from orders.models import OrderItemDetail, OrderItem, Order, OrderUserProfile


class OrderItemDetailSchema(ModelSchema):
    model_config = ConfigDict(model=OrderItemDetail)


class OrderItemSchema(ModelSchema):
    details: List[OrderItemDetailSchema]
    model_config = ConfigDict(model=OrderItem)


class OrderSchema(ModelSchema):
    items: List[OrderItemSchema]
    model_config = ConfigDict(model=Order)


class OrderUserProfileSchema(ModelSchema):
    model_config = ConfigDict(model=OrderUserProfile)


class OrderUserSchema(ModelSchema):
    orders: List[OrderSchema]
    profile: OrderUserProfileSchema
    model_config = ConfigDict(model=OrderUser)

Now let's assume you're interested in exporting the order and profile information for a particular user into a JSON format that contains the details accross all of the related item objects:

user = OrderUser.objects.first()
print(OrderUserSchema.from_django(user).json(ident=4))

Output:

{
    "profile": {
        "id": 1,
        "address": "",
        "user": 1
    },
    "orders": [
        {
            "items": [
                {
                    "details": [
                        {
                            "id": 1,
                            "name": "",
                            "order_item": 1
                        }
                    ],
                    "id": 1,
                    "price": 0.0,
                    "quantity": 0,
                    "order": 1
                }
            ],
            "id": 1,
            "total_price": 0.0,
            "user": 1
        }
    ],
    "id": 1,
    "email": ""
}

The model schema definitions are composable and support customization of the output according to the auto-generated fields and any additional annotations.

Including and excluding fields

The fields exposed in the model instance may be configured using two options: include and exclude. These represent iterables that should contain a list of field name strings. Only one of these options may be set at the same time, and if neither are set then the default behaviour is to include all of the fields from the Django model.

For example, to include all of the fields from a user model except a field named email_address, you would use the exclude option:

from pydantic import ConfigDict

class UserSchema(ModelSchema):
    model_config = ConfigDict(model=User, exclude=["email_address"])

In addition to this, you may also limit the fields to only include annotations from the model schema class by setting the include option to a special string value: "__annotations__".

from pydantic import ConfigDict

class ProfileSchema(ModelSchema):
        website: str
        model_config = ConfigDict(model=Profile, include="__annotations__")


    assert ProfileSchema.schema() == {
        "title": "ProfileSchema",
        "description": "A user's profile.",
        "type": "object",
        "properties": {
            "website": {
                "title": "Website",
                "type": "string"
            }
        },
        "required": [
            "website"
        ]
    }

djantic2's People

Contributors

dependabot[bot] avatar jonathan-s avatar jordaneremieff avatar marcy-buccellato avatar mmcardle avatar phbernardes avatar xshapira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

djantic2's Issues

Thread safety issue in `from_django`

Hi, I was looking into Pydantic <-> DRF interop and found this library, thanks for working on it.

From a cursory look at the code, I noticed a thread safety issue in the from_django classmethod:

djantic2/djantic/main.py

Lines 253 to 267 in 65e717b

@classmethod
def from_django(cls, objs, many=False, context={}, dump=False):
# TODO is context really passed into model_validate, test this
cls.context = context
if many:
result_objs = []
for obj in objs:
cls.instance = obj
obj = ProxyGetterNestedObj(obj, cls)
instance = cls(**obj.dict())
result_objs.append(cls.model_validate(instance))
return result_objs
cls.instance = objs
# NOTE question mark around the code above.

The function mutates a couple of globals (class variables) instance and context without any locking, which means it will break when running in multiple threads concurrently (a common way to run Django).

  • cls.instance - I'm not sure how it's used (I don't see any direct accesses at least) so not sure about it, but it seems odd to save the instance on the class...
  • cls.context - I'm not sure as well, though I wonder why it's not just passed directly as cls.model_validate(..., context=context).

Pydantic 2.7.0 breaks tests

As evidenced in #2 the latest version of pydantic started to fail the tests. We should try to work around that compatibility and find the source of that failure.

Add package updates to Releases in Github

Thanks for this fork! I was bummed to see Djantic abandoned(ish), then thrilled to see your issue posted there about this fork.

I always "watch" the releases on a repo for a package I use - would you mind using that feature when you push a new version so we're prompted to come see what's new?

Thanks again!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.