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

What about casting CRUD returned value to pydantic models ? #15

Closed
ebreton opened this issue Apr 24, 2019 · 2 comments
Closed

What about casting CRUD returned value to pydantic models ? #15

ebreton opened this issue Apr 24, 2019 · 2 comments

Comments

@ebreton
Copy link
Contributor

ebreton commented Apr 24, 2019

I have implemented

  • a pydantic City model (and some extra models for the updates, creations as suggested in the tutorial).
  • a SQLalchemy DBCity model

I would like the crud.get method to return the City model and not the DBCity.

More important, I would like the casting from DBCity to City to be done automatically, with something like

def get(db_session, city_id: int) -> Optional[City]:
    return db_session.query(DBCity).get(city_id)

The code above does not make any casting, and behaves as follow:

def get(db_session, city_id: int) -> Optional[DBCity]:
    return db_session.query(DBCity).get(city_id)

What do you think of this behavior ?

Wouldn't it be nice to have an auto-casting like the ones of the API ?

@ebreton
Copy link
Contributor Author

ebreton commented May 1, 2019

After a few days of playing around, I finnaly think that it is appropriate that crud methods returns SQLAlchemy objects. The casting to pydantic objects should be done later on by the layers above

@ebreton ebreton closed this as completed May 1, 2019
@tiangolo
Copy link
Owner

Cool, thanks for reporting back and closing the issue.

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

No branches or pull requests

2 participants