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

Conversation

dantownsend
Copy link
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
@dantownsend dantownsend added this to In progress in Enhancements via automation Mar 24, 2022
@codecov-commenter
Copy link

codecov-commenter commented Mar 24, 2022

Codecov Report

Merging #469 (ce4f905) into master (52f01ae) will decrease coverage by 0.01%.
The diff coverage is 75.00%.

@@            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     
Impacted Files Coverage Δ
piccolo/utils/pydantic.py 96.87% <75.00%> (-1.00%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 52f01ae...ce4f905. Read the comment docs.

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

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
Enhancements
In progress
Development

Successfully merging this pull request may close these issues.

None yet

3 participants