Serializer
You can write your serializer
in ۳ style:
Style 1 (Pydantic)#
Write a normal pydantic
class and use it as serializer:
from pydantic import BaseModel
from pydantic import Field
from panther.app import API
from panther.request import Request
from panther.response import Response
class UserSerializer(BaseModel):
username: str
password: str
first_name: str = Field(default='', min_length=2)
last_name: str = Field(default='', min_length=4)
@API(input_model=UserSerializer)
async def serializer_example(request: Request):
return Response(data=request.validated_data)
Style 2 (Model Serializer)#
Use panther ModelSerializer
to write your serializer which will use your model
to create fields.
from pydantic import Field
from panther import status
from panther.app import API
from panther.db import Model
from panther.request import Request
from panther.response import Response
from panther.serializer import ModelSerializer
class User(Model):
username: str
password: str
first_name: str = Field(default='', min_length=2)
last_name: str = Field(default='', min_length=4)
# type 1 - using fields
class UserModelSerializer(ModelSerializer):
class Config:
model = User
fields = ['username', 'first_name', 'last_name']
required_fields = ['first_name']
# type 2 - using exclude
class UserModelSerializer(ModelSerializer):
class Config:
model = User
fields = '*'
required_fields = ['first_name']
exclude = ['id', 'password']
@API(input_model=UserModelSerializer)
async def model_serializer_example(request: Request):
return Response(data=request.validated_data, status_code=status.HTTP_202_ACCEPTED)
Notes#
- In the example above,
ModelSerializer
will look up for the value ofConfig.fields
in theUser.model_fields
and use theirtype
andvalue
for thevalidation
. Config.model
andConfig.fields
arerequired
when you are usingModelSerializer
.- You can force a field to be required with
Config.required_fields
- You can force a field to be optional with
Config.optional_fields
Config.required_fields
andConfig.optional_fields
can't include same fields- If you want to use
Config.required_fields
orConfig.optional_fields
you have to put their value inConfig.fields
too. Config.fields
&Config.required_fields
&Config.optional_fields
can be*
too (Include all the model fields)Config.exclude
is mostly used whenConfig.fields
is'*'
Style 3 (Model Serializer + Pydantic)#
You can use pydantic.BaseModel
features in ModelSerializer
too.
from pydantic import Field, field_validator, ConfigDict
from panther import status
from panther.app import API
from panther.db import Model
from panther.request import Request
from panther.response import Response
from panther.serializer import ModelSerializer
class User(Model):
username: str
password: str
first_name: str = Field(default='', min_length=2)
last_name: str = Field(default='', min_length=4)
class UserModelSerializer(ModelSerializer):
model_config = ConfigDict(str_to_upper=True)
age: int = Field(default=20)
is_male: bool
username: str
class Config:
model = User
fields = ['first_name', 'last_name']
required_fields = ['first_name']
optional_fields = ['last_name']
@field_validator('username')
def validate_username(cls, username):
print(f'{username=}')
return username
@API(input_model=UserModelSerializer)
async def model_serializer_example(request: Request):
return Response(data=request.validated_data, status_code=status.HTTP_202_ACCEPTED)
Notes#
- You can add custom
fields
- You can add
model_config
asattribute
or asConfig
- You can use
@field_validator
and othervalidators
ofpydantic
.