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

Support nested model created with create_pydantic_model #76

Closed
dantownsend opened this issue Aug 29, 2021 · 4 comments · Fixed by #77
Closed

Support nested model created with create_pydantic_model #76

dantownsend opened this issue Aug 29, 2021 · 4 comments · Fixed by #77

Comments

@dantownsend
Copy link
Member

As suggested here:

piccolo-orm/piccolo#202

It would be great if create_pydantic_model could generate nested models.

create_pydantic_model(Band, nested_models=[Band.manager])

This would output something equivalent to this:

from pydantic import BaseModel

class ManagerModel:
    name: str    

class BandModel:
    name: str
    manager: ManagerModel
@aminalaee
Copy link
Member

@dantownsend This is cool, I just have a question, should create_pydantic_model now return a list of models? In case of nested models returning two?

@dantownsend
Copy link
Member Author

@aminalaee Yeah, it's a good point. I think it would just return a single model still. The child models would just be created internally by create_pydantic_model.

It's a bit ugly, but you could still access the sub models if you wanted them.

class Manager(Table):
    name = Varchar()

class Band(Table):
    name = Varchar()
    manager = ForeignKey(Manager)

BandModel = create_pydantic_model(Band, nested=True)
BandModel.__fields__['manager'].type_
>>> ManagerModel

What do you think?

@aminalaee
Copy link
Member

@dantownsend I have to say I like BandModel = create_pydantic_model(Band, nested=True) better than previous ones.
Let me try it out.

@dantownsend
Copy link
Member Author

@aminalaee Great, thanks

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 a pull request may close this issue.

2 participants