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

prototype Pydantic choice support in create_pydantic_model #469

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions piccolo/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ def create_pydantic_model(
#######################################################################
# Work out the column type

if isinstance(column, (Decimal, Numeric)):
value_type: t.Type = pydantic.condecimal(
if column._meta.choices:
value_type: t.Type = column._meta.choices
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dantownsend Strings work well. Adding these two lines code becomes compatible with Piccolo Admin (with field propertyTitle).

# pydantic.py line 242
if column._meta.choices:
    params["title"] = column._meta.choices.__name__

In Piccolo Admin change this line to v-bind:type="property.type" and this line to class Gender(str, enum.Enum): and everything work well in admin and openapi docs.

Unfortunately ints doesn't work due to validation error.
error

elif isinstance(column, (Decimal, Numeric)):
value_type = pydantic.condecimal(
max_digits=column.precision, decimal_places=column.scale
)
elif isinstance(column, Varchar):
Expand Down