piccolo icon indicating copy to clipboard operation
piccolo copied to clipboard

prototype Pydantic choice support in `create_pydantic_model`

Open dantownsend opened this issue 3 years ago • 1 comments

Related to https://github.com/piccolo-orm/piccolo/issues/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 https://github.com/piccolo-orm/piccolo/issues/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 avatar Mar 24 '22 10:03 dantownsend

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%) :arrow_down:

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.

codecov-commenter avatar Mar 24 '22 11:03 codecov-commenter