-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add pydantic model creation method to Gino Model #688
base: master
Are you sure you want to change the base?
Conversation
Adds a class method 'get_pydantic_model' to the base Model class that introspects the class to provide a pydantic model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pydantic should be declared as optional dependency and should be imported only in case you are using corresponding methods
Sorry, I was in a move in the last few days and everything is a bit messy. I Will take a look this weekend. Thanks for the PR! |
Any updates/thoughts/concerns? Very actual thing |
Sorry, I've been a bit busy lately at work. I guess it's similar situation for Fantix. Currently I'm limited to check issues but not PRs. I'll try my best to look into it, but also as this is WIP, likely this won't be merged into master soon. :( |
keys = [str(key) for key in cls.__dict__.keys()] | ||
# Assumption that may not be valid, but don't look at ones with _ in them. | ||
valid_keys = [key for key in keys if not key.startswith('_')] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better if you use cls.__table__.columns.keys()
method here.
Since the author still hasn't merged, and if someone's looking for the same feature, I've made a simple replacement, just copypaste it (note: there's python3.9 typing): from typing import Optional, Type
from gino.declarative import Model
from pydantic import BaseModel, create_model
def gino_to_pydantic(
model: Type[Model],
*,
include: Optional[list[str]] = None,
exclude: Optional[list[str]] = None,
) -> Type[BaseModel]:
"""Convert model to pydantic class representation.
:param model: gino model
:type model: Type[Model]
:param include: fields to include, defaults to None,
if None, all fields are included
:type include: Optional[list[str]], optional
:param exclude: excludes fields form included, defaults to None
:type exclude: Optional[list[str]], optional
:return: pydantic model class with casted gino fields
:rtype: Type[BaseModel]
"""
if include is None:
include = model.__table__.columns.keys()
if exclude is not None:
include = [field for field in include if field not in exclude]
return create_model(model.__name__, **{ # type: ignore
str(column.name): (column.type.python_type, ...)
for column in model.__table__.columns if str(column.name) in include
}) |
Adds a class method 'get_pydantic_model' to the base Model class
that introspects the class to provide a pydantic model.
Questions that still need answers/feedback:
Things to do: