tortoise / tortoise/tortoise-orm
数据库字段设置null=True时,通过pydantic_model_creator还是要必填
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Describe the bug
数据库字段设置null=True时,通过pydantic_model_creator还是要必填。
When the database field is set to null=True, use pydantic_model_creator is still required.
To Reproduce
class MyTable(Model):
name = fields.CharField(max_length=255, description='名称')
cancel_time = fields.DatetimeField(description='取消时间', default=None, null=True)
MyTableModel = pydantic_model_creator(MyTable, name='MyTable', exclude_readonly=True, model_config=model_config)
MyTableModel(name="abc") # raise ValidationError: missing `cancel_time`
Expected behavior
对于tortoise-orm中允许为空的字段,创建Pydantic对象应该对该字段允许为空
For fields that are allowed to be empty in the Tortoise orm, creating a Pydantic object should allow this field to be empty
Additional context
我当前在自己本地修改的方式:
My current method of modifying locally is:
if fname in properties and not isinstance(properties[fname], tuple):
fconfig["title"] = fname.replace("_", " ").title()
description = comment or _br_it(fdesc.get("docstring") or fdesc["description"] or "")
if description:
fconfig["description"] = description
ftype = properties[fname]
if isinstance(ftype, PydanticDescriptorProxy):
continue
# if field_default is not None and not callable(field_default): # Current writing
if (fdesc.get("nullable") or field_default is not None) and not callable(field_default): # My writing
properties[fname] = (ftype, Field(default=field_default, **fconfig))
else:
properties[fname] = (ftype, Field(**fconfig))
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the pydantic_model_creator implementation and reproduce the MyTable example with a nullable cancel_time field. Trace how nullable fields and defaults become Pydantic fields, then verify that creating the model without cancel_time no longer raises a missing-field validation error and add regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100