Coder Social home page Coder Social logo

drf-pydantic's Introduction

CI/CD Status Test Coverage PyPI version

Use pydantic with Django REST framework

Introduction

Pydantic is a Python library used to perform data serialization and validation.

Django REST framework is a framework built on top of Django which allows writing REST APIs.

If like me you develop DRF APIs and you like pydantic , drf-pydantic is for you ๐Ÿ˜.

Installation

pip install drf-pydantic

Usage

General

Use drf_pydantic.BaseModel instead of pydantic.BaseModel when creating your models:

from drf_pydantic import BaseModel

class MyModel(BaseModel):
  name: str
  addresses: list[str]

Whenever you need a DRF serializer you can get it from the model like this:

MyModel.drf_serializer

โ„น๏ธ INFO
Models created using drf_pydantic are fully idenditcal to those created by pydantic. The only change is the addition of the drf_serializer attribute during class creation (not instance).

Existing Models

If you have an existing code base and you would like to use the drf_serializer attribute to only specific models, then great news ๐Ÿฅณ - you can easily extend your existign pydantic models by adding drf_pydantic.BaseModel to the list of parent classes.

Your existing pydantic models:

from pydantic import BaseModel

class Pet(BaseModel):
  name: str

class Dog(Pet):
  breed: str

Update your Dog model and get serializer via the drf_serializer:

from drf_pydantic import BaseModel as DRFBaseModel
from pydantic import BaseModel

class Pet(BaseModel):
  name: str

class Dog(DRFBaseModel, Pet):
  breed: str

Dog.drf_serializer

โš ๏ธ ATTENTION
Inheritance order is important: drf_pydantic.BaseModel must always go before the pydantic.BaseModel class.

Nested Models

If you have nested models and you want to generate serializer only from one of them, you don't have to update all models - only update the model you need, drf_pydantic will generate serializers for all normal nested pydantic models for free ๐Ÿฑโ€๐Ÿ‘ค.

from drf_pydantic import BaseModel as DRFBaseModel
from pydantic import BaseModel

class Apartment(BaseModel):
  floor: int
  tenant: str

class Building(BaseModel):
  address: str
  aparments: list[Apartment]

class Block(DRFBaseModel):
  buildings: list[Buildind]

Block.drf_serializer

Roadmap

  • Add ENUM support
  • Add option to create custom serializer for complex models
  • Add support for constraints (max, min, regex, etc.)

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.