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

Optional parameter in pydantic_model_creator does not work after upgrading to pydantic v2 #1551

Closed
cary997 opened this issue Jan 25, 2024 · 2 comments

Comments

@cary997
Copy link

cary997 commented Jan 25, 2024

Describe the bug

  • tortoise-orm = 0.20.0
  • pydantic==2.5.3
  • pydantic-core==2.14.6

升级至 pydantic v2 后 使用 pydantic_model_creator 创建 pydantic 模型时,pydantic_model_creator(optional=(xxx))不生效,字段 仍为必须填写

After upgrading to pydantic v2, when using pydantic_model_creator to create pydantic model, pydantic_model_creator(optional=(xxx)) does not take effect, and fields are still required.

To Reproduce

class AuthUsers(BaseModel):
    username = fields.CharField(max_length=32, unique=True)
    password = fields.CharField(max_length=128)
    nickname = fields.CharField(max_length=32)
    phone = fields.CharField(null=True, max_length=20, unique=True)
    email = fields.CharField(max_length=128, unique=True)

    class Meta:
        table = "auth_users"
        indexes = ("username", "user_status")

class UserUpdateRequest(pydantic_model_creator(
    cls=AuthUsers,
    name="UserUpdateRequest",
    exclude=("username", "password",),
    exclude_readonly=True,
    optional=("nickname", "email",)
))
image

Expected behavior
在UserUpdateRequest模型中,nickname和email应该为可选的,但实际为必填参数。在pydantic v1和tortoise-orm 0.19.3中是正常工作的

In the UserUpdateRequest model, nickname and email should be optional, but are actually required parameters. This is working fine in pydantic v1 and tortoise-orm 0.19.3

Additional context
我已经下载develop分支中的最新源码,仍然存在此问题,我在/tortoise/contrib/pydantic/creator.py文件中看到以下代码,当我添加了json_schema_extra["nullable"] = True时,工作正常

I have downloaded the latest source code in the develop branch and still have this problem, I see the following code in the /tortoise/contrib/pydantic/creator.py file and when I add json_schema_extra["nullable"] = True, it works fine

image image

我认为是pydantic v2迁移指南中描述的一些更改引起的 Pydantic 2.0 Migration Guide

I think it's caused by some changes described in the pydantic v2 migration guide Pydantic 2.0 Migration Guide

@YuriFontella
Copy link

Has this been resolved somehow?

waketzheng added a commit to waketzheng/tortoise-orm that referenced this issue May 1, 2024
…or pydantic v2. (tortoise#1551)
abondar pushed a commit that referenced this issue May 7, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
#1599)

* fix: `optional` parameter in `pydantic_model_creator` does not work for pydantic v2. (#1551)

* Fix test

* Add new test case
@abondar
Copy link
Member

abondar commented May 24, 2024

Should be fixed in 0.21.0

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

3 participants