Skip to content

prototype Pydantic choice support in create_pydantic_model#469

Open
dantownsend wants to merge 1 commit intomasterfrom
467-pydantic-choices
Open

prototype Pydantic choice support in create_pydantic_model#469
dantownsend wants to merge 1 commit intomasterfrom
467-pydantic-choices

Conversation

@dantownsend
Copy link
Copy Markdown
Member

Related to #467

Just a prototype at the moment.

If a Piccolo column has choices, we can use the choices Enum as the Pydantic type. For example:

class RelationshipType(str, enum.Enum):
    colleague = "colleague"
    friend = "friend"

class Person(Table):
    type = Varchar(choices=RelationshipType)    

It would become this Pydantic model:

class PersonModel(BaseModel):
    type: RelationshipType

The problem is the JSON schema it generates is quite complex, and Piccolo Admin doesn't currently understand $ref values:

Screenshot 2022-03-24 at 10 17 21

So the options are:

  1. Modify Piccolo Admin so it understands $ref
  2. Start work on PydanticModel class #331, which would add a new API for creating Pydantic models. create_pydantic_model will still be supported, and used by Piccolo admin for now, but we can migrate to this new API at a later date.

@dantownsend dantownsend added the enhancement New feature or request label Mar 24, 2022
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 24, 2022

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.83%. Comparing base (52f01ae) to head (ce4f905).
⚠️ Report is 459 commits behind head on master.

Files with missing lines Patch % Lines
piccolo/utils/pydantic.py 75.00% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #469      +/-   ##
==========================================
- Coverage   90.84%   90.83%   -0.02%     
==========================================
  Files         104      104              
  Lines        6913     6915       +2     
==========================================
+ Hits         6280     6281       +1     
- Misses        633      634       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

if isinstance(column, (Decimal, Numeric)):
value_type: t.Type = pydantic.condecimal(
if column._meta.choices:
value_type: t.Type = column._meta.choices
Copy link
Copy Markdown
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants