Skip to content

How to minimize type declarations? #912

Answered by dantownsend
waldner asked this question in Q&A
Discussion options

You must be logged in to vote

If you're dealing with Pydantic classes, you could do something like:

from piccolo.columns import Varchar
from piccolo.tables import Table
from piccolo.utils.pydantic import create_pydantic_model

class User(Table):
    name = Varchar()


class UserModel(create_pydantic_model(User)):
    def my_custom_method(self):
        ...

So starting from the Piccolo table, and then creating the Pydantic model for exchanging data with other services.

You can then add the validation to the Pydantic model. Creating Piccolo table instances from the Pydantic model should then be as simple as:

piccolo_user_object = User(**UserModel.model_dump())

Likewise you can go from Piccolo table instances to Pydantic…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@waldner
Comment options

Answer selected by waldner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants