prototype Pydantic choice support in `create_pydantic_model`
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:
So the options are:
- Modify Piccolo Admin so it understands
$ref - Start work on https://github.com/piccolo-orm/piccolo/issues/331, which would add a new API for creating Pydantic models.
create_pydantic_modelwill still be supported, and used by Piccolo admin for now, but we can migrate to this new API at a later date.
Codecov Report
Merging #469 (ce4f905) into master (52f01ae) will decrease coverage by
0.01%. The diff coverage is75.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 dataPowered by Codecov. Last update 52f01ae...ce4f905. Read the comment docs.