question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pydantic_model_creator is not generating tortoise.field.CharEnumField as enumeration

See original GitHub issue

Describe the bug I am currently using the method pydantic_model_creator from tortoise.contrib.pydantic to generate my pydantic model from my tortoise model.

However, the generation is not behaving as I would expect it to be for the tortoise field CharEnumField. The pydantic model generated from a tortoise model containing a CharEnumField is set as a Char field and not an Enum field. The OpenApi schema generated is:

"my_enum": {
    "title": "My Enum",
    "description": "A: a<br/>B: b<br/>C: c",
    "maxLength": 1,
    "type": "string"
}

The OpenApi schema generated from a pydantic mode (that is not created from a tortoise model) results in the following:

"MyEnum": {
    "title": "MyEnum",
    "description": "An enumeration.",
    "enum": [
        "a",
        "b",
        "c"
    ],
    "type": "string"
}

To Reproduce

from enum import Enum

from pydantic import BaseModel
from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise.models import Model


class MyEnum(str, Enum):
    A = "a"
    B = "b"
    C = "c"


class MyModel(Model):
    my_enum = fields.CharEnumField(MyEnum)


class MyModelPydantic(BaseModel):
    my_enum: MyEnum


MyModelPydanticCreator = pydantic_model_creator(MyModel, name="MyModelPydanticCreator")

print(MyModelPydanticCreator.schema_json(indent=2))
# Output :
# {
#     "title": "MyModelPydanticCreator",
#     "type": "object",
#     "properties": {
#         "id": {
#             "title": "Id",
#             "minimum": 1,
#             "maximum": 2147483647,
#             "type": "integer"
#         },
#         "my_enum": {
#             "title": "My Enum",
#             "description": "A: a<br/>B: b<br/>C: c",
#             "maxLength": 1,
#             "type": "string"
#         }
#     },
#     "required": [
#         "id",
#         "my_enum"
#     ],
#     "additionalProperties": false
# }

print(MyModelPydantic.schema_json(indent=2))
# Output :
# {
#     "title": "MyModelPydantic",
#     "type": "object",
#     "properties": {
#         "my_enum": {
#             "$ref": "#/definitions/MyEnum"
#         }
#     },
#     "required": [
#         "my_enum"
#     ],
#     "definitions": {
#         "MyEnum": {
#             "title": "MyEnum",
#             "description": "An enumeration.",
#             "enum": [
#                 "a",
#                 "b",
#                 "c"
#             ],
#             "type": "string"
#         }
#     }
# }

Expected behaviour I would expect that both ways of creating the pydantic model result in the same OpenApi schema/Pydantic model, i.e. using an enum field and not a basic char field.

Additional context I am using FastApi / Tortoise-Orm / Pydantic. I came across this problem because my pydantic model (which is the body requested by my FastApi endpoint) was letting strings that are not in my enum through the endpoint entry.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

1reaction
ref0xcommented, Jan 12, 2021

Looking for the same solution

0reactions
simsim0709commented, Dec 6, 2022

I have same problem. Any update?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to serialise CharEnumField · Issue #576 · tortoise ...
Describe the bug Inability to serialise a field of type CharEnumField due to ValidationError To Reproduce Example: ## Lets say we have a ......
Read more >
Fields — Tortoise ORM v0.17.3 Documentation
Returns True if the field is required to be provided. It needs to be non-nullable and not have a default or be DB-generated...
Read more >
Changelog - Tortoise ORM v0.19.3 Documentation
This will create model instances that only have those values fetched. Persisting changes on the model is allowed only when: All the fields...
Read more >
Pydantic enum field does not get converted to string
I am trying to restrict one field in a class to an enum. However, when I try to get a dictionary out of...
Read more >
Working with Enum Fields | Apache Solr Reference Guide 7.0
EnumFieldType allows defining a field whose values are a closed set, and the sort order is pre-determined but is not alphabetic nor numeric....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found