Skip to content

Commit

Permalink
Add Serializer class
Browse files Browse the repository at this point in the history
  • Loading branch information
citrux committed Oct 20, 2020
1 parent f7fa638 commit a7a15d4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions joey/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pydantic import BaseModel, Field
from pydantic.main import ModelMetaclass
from pydantic.utils import GetterDict
from typing import List, Optional, Any


class NestedGetterDict(GetterDict):
def get(self, key: Any, default: Any = None):
res = self._obj
for s in key.split('.'):
res = getattr(res, s, default)
return res


class SerializerMetaclass(ModelMetaclass):
def __call__(cls, obj):
return cls.from_orm(obj)


class Serializer(BaseModel, metaclass=ModelMetaclass):
class Config:
orm_mode = True
getter_dict = NestedGetterDict

0 comments on commit a7a15d4

Please sign in to comment.