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

pydantic_model_creator include has a bug #1430

Closed
Hillsir opened this issue Jul 19, 2023 · 12 comments
Closed

pydantic_model_creator include has a bug #1430

Hillsir opened this issue Jul 19, 2023 · 12 comments

Comments

@Hillsir
Copy link

Hillsir commented Jul 19, 2023

pydantic_model_creator has a bug

When I use include in pydantic_model_creator to get fk_field I get an error, KeyError: 'fk_model_id'

this error

Traceback (most recent call last):
  File "F:\Python\plus_python\apps\llmapp\views\__init__.py", line 46, in <module>
    error_test = pydantic_queryset_creator(Tournament, include=("fk_model",), name='test2')
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\envs\plus_python\Lib\site-packages\tortoise\contrib\pydantic\creator.py", line 483, in pydantic_queryset_creator
    submodel = pydantic_model_creator(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\envs\plus_python\Lib\site-packages\tortoise\contrib\pydantic\creator.py", line 304, in pydantic_model_creator
    field_map_update(included_fields)
  File "F:\envs\plus_python\Lib\site-packages\tortoise\contrib\pydantic\creator.py", line 284, in field_map_update
    del field_map[raw_field]
        ~~~~~~~~~^^^^^^^^^^^
KeyError: 'fk_model_id'

this mini code

from tortoise import Tortoise, fields, run_async
from tortoise.contrib.pydantic import pydantic_queryset_creator, pydantic_model_creator
from tortoise.models import Model


class FKModel(Model):
    name = fields.CharField(max_length=100)


class Tournament(Model):
    """
    This references a Tournament
    """

    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=100)
    #: The date-time the Tournament record was created at
    created_at = fields.DatetimeField(auto_now_add=True)

    fk_model = fields.ForeignKeyField('models.FKModel', related_name='tournaments')


Tortoise.init_models(["__main__"], "models")

# The following code uses Include to specify the required fields. If any ForeignKeyField appears in the fields, an error will be raised.
"""
Traceback (most recent call last):
  File "F:\Python\plus_python\apps\llmapp\views\__init__.py", line 43, in <module>
    error_test = pydantic_queryset_creator(Tournament, include=("fk_model",), name='test2')
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\envs\plus_python\Lib\site-packages\tortoise\contrib\pydantic\creator.py", line 483, in pydantic_queryset_creator
    submodel = pydantic_model_creator(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\envs\plus_python\Lib\site-packages\tortoise\contrib\pydantic\creator.py", line 304, in pydantic_model_creator
    field_map_update(included_fields)
  File "F:\envs\plus_python\Lib\site-packages\tortoise\contrib\pydantic\creator.py", line 284, in field_map_update
    del field_map[raw_field]
        ~~~~~~~~~^^^^^^^^^^^
KeyError: 'fk_model_id'
"""

error_test = pydantic_queryset_creator(Tournament, include=("fk_model",), name='test2')
print(error_test.schema_json(indent=1, ))

# but exclude works fine
success_test = pydantic_model_creator(Tournament, exclude=("id", 'name', 'created_at',), include=('fk_model',), name='test1')
print(success_test.schema_json(indent=1, ))


async def run():
    await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
    await Tortoise.generate_schemas()


if __name__ == "__main__":
    run_async(run())
@rilshok
Copy link

rilshok commented Jul 20, 2023

I was trying to understand the logic behind the pydantic_model_creator function, it is very difficult to understand. I think it should be refactored. Moreover, it does not work with pydantic=2.0.3

@Hillsir
Copy link
Author

Hillsir commented Jul 20, 2023

I was trying to understand the logic behind the pydantic_model_creator function, it is very difficult to understand. I think it should be refactored. Moreover, it does not work with pydantic=2.0.3

Yes, I think there's a certain gap between what he was designed to do and what he's shown to be able to do, and I have to dump it.....

@vinicius-oa
Copy link

I was trying to understand the logic behind the pydantic_model_creator function, it is very difficult to understand. I think it should be refactored. Moreover, it does not work with pydantic=2.0.3

Yeah, I tried to execute the tutorials with that pydantic's version and I get :


File "venv/lib/python3.10/site-packages/tortoise/contrib/pydantic/creator.py", line 437, in pydantic_model_creator
    model = cast(Type[PydanticModel], type(_name, (PydanticModel,), properties))
  File "venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 97, in __new__
    private_attributes = inspect_namespace(
  File "venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 298, in inspect_namespace
    and value.__module__ == namespace['__module__']
KeyError: '__module__'

@long2ice
Copy link
Member

Try latest, pydantic2.0 is supported

@PeiPeiWow
Copy link

I was trying to understand the logic behind the pydantic_model_creator function, it is very difficult to understand. I think it should be refactored. Moreover, it does not work with pydantic=2.0.3

Yeah, I tried to execute the tutorials with that pydantic's version and I get :


File "venv/lib/python3.10/site-packages/tortoise/contrib/pydantic/creator.py", line 437, in pydantic_model_creator
    model = cast(Type[PydanticModel], type(_name, (PydanticModel,), properties))
  File "venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 97, in __new__
    private_attributes = inspect_namespace(
  File "venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 298, in inspect_namespace
    and value.__module__ == namespace['__module__']
KeyError: '__module__'

@PeiPeiWow
Copy link

File "/Users/qilong/Desktop/fastApiUser/main.py", line 15, in
from app.models import User, Token, Token_Pydantic, TokenIn_Pydantic, User_Pydantic, UserIn_Pydantic
File "/Users/qilong/Desktop/fastApiUser/app/models.py", line 34, in
User_Pydantic = pydantic_model_creator(User, name="User")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/qilong/Desktop/fastApiUser/venv/lib/python3.11/site-packages/tortoise/contrib/pydantic/creator.py", line 437, in pydantic_model_creator
model = cast(Type[PydanticModel], type(_name, (PydanticModel,), properties))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/qilong/Desktop/fastApiUser/venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 97, in new
private_attributes = inspect_namespace(
^^^^^^^^^^^^^^^^^^
File "/Users/qilong/Desktop/fastApiUser/venv/lib/python3.11/site-packages/pydantic/_internal/_model_construction.py", line 298, in inspect_namespace
and value.module == namespace['module']
~~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'module'

I have met same problem, how to solve, does anyone has solution?

@PeiPeiWow
Copy link

Try latest, pydantic2.0 is supported

not work

@Hillsir
Copy link
Author

Hillsir commented Jul 22, 2023

Try latest, pydantic2.0 is supported

not work

can my mini code run?

@PeiPeiWow
Copy link

Try latest, pydantic2.0 is supported

OK, I fixed the bug but not use pydantic==2.0.
Here is why:

When I downgraded pydantic==2.0.0, I got this
image

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
fastapi 0.100.0 requires pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,<3.0.0,>=1.7.4, but you have pydantic 2.0 which is incompatible.

so I have to install pydantic==1.10.11, and this solve the problem.

@PeiPeiWow
Copy link

Try latest, pydantic2.0 is supported

not work

can my mini code run?

Oh, I don't try your solution which is the mini code you mentioned. I have found a way to fix this bug, just use pydantic==1.10.11

@PeiPeiWow
Copy link

Try latest, pydantic2.0 is supported

Hi, compatible versions of pydantic which are 2.0.2, 2.0.3, and 2.1.1, but tortoise orm still has bugs with these pydantic v2 versions. Please help.

@Abdeldjalil-H
Copy link
Contributor

I think this should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants