Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bubthegreat
Copy link

@bubthegreat bubthegreat commented May 31, 2020

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:

  • Do we have enough context on a model to follow relations without additional dependencies?
  • Is this the right place for this method, or should it live somewhere else or in a new utility module?
  • Is there a better way to introduce this to the classes, like with a consistent dunder method that we can utilize elsewhere, etc.

Things to do:

  • Follow relations with a recursion limit
  • Test more complex models
  • Add lots of tests

Adds a class method 'get_pydantic_model' to the base Model class
that introspects the class to provide a pydantic model.
Copy link

@IvanLarinAtSpark IvanLarinAtSpark left a 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

@fantix
Copy link
Member

fantix commented Jun 5, 2020

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!

@xnuinside
Copy link
Contributor

Any updates/thoughts/concerns? Very actual thing

@wwwjfy
Copy link
Member

wwwjfy commented Oct 18, 2020

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. :(

Comment on lines +400 to +402
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('_')]
Copy link

@Mastermind-U Mastermind-U Feb 24, 2021

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.

@Mastermind-U
Copy link

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
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants