Skip to content

VitailOG/pydantic-meta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pydantic-Meta


About

Allow to define models where many similar fields

Installation

 pip install pydantic-meta

Example

  • Partial

Traditional define

from pydantic import BaseModel

class User(BaseModel):
    name: str | None
    age: int | None
    
User(name=None, age=None)

With metaclass Partial

from pydantic import BaseModel
from pydantic_meta.partial import Partial

class User(BaseModel, metaclass=Partial):
    name: str
    age: int
    
User(name=None, age=None)
  • Required

Traditional define

if you want the fields in the parent model to be mandatory in the traditional announcement, it is not possible.

from pydantic import BaseModel

class Name(BaseModel):
    name: str | None = None
    
class User(Name):
    age: int | None
    
User(name="John", age=12)

With metaclass Required

from pydantic import BaseModel
from pydantic_meta.required import Required

class Name(BaseModel):
    name: str | None = None
    
class User(Name, metaclass=Required):
    age: int | None
    
    class Config:
        required_fields=['name']
    
User(name="John", age=12)

or

from pydantic import BaseModel
from pydantic_meta.required import Required

class Name(BaseModel):
    name: str | None = None
    
class User(Name, metaclass=Required, required_fields=['name']):
    age: int | None
    
User(name="John", age=12)
  • Pick

Traditional define

from pydantic import BaseModel

class User(BaseModel):
    name: str
    first_name: str | None = None
    age: int | None = None
    
User(name="John")

With metaclass Pick

from pydantic import BaseModel
from pydantic_meta.pick import Pick

class User(BaseModel, metaclass=Pick):
    name: str
    first_name: str 
    age: int
    
    class Config:
        pick_fields = ['name']
    
User(name="John")

or

from pydantic import BaseModel
from pydantic_meta.pick import Pick

class User(BaseModel, metaclass=Pick, pick_fields=['name']):
    name: str
    first_name: str
    age: int
    
User(name="John")

by default, the fields are set to None, and you can change this by specifying the default_none=False attribute

from pydantic import BaseModel
from pydantic_meta.pick import Pick

class User(BaseModel, metaclass=Pick, pick_fields=['name'], default_none=False):
    name: str
    first_name: str
    age: int
    
User(name="John", first_name=None, age=None)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages