tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.37k stars 355 forks source link

Refactoring pydantic_model_creator for Enhanced Code Maintainability and Reliability #1622

Closed zhaowanpeng closed 1 month ago

zhaowanpeng commented 1 month ago

My code: ` from tortoise import models, fields

class Channel(models.Model): id = fields.IntField(pk=True, readonly=True) create_user_id = fields.IntField(null=True, readonly=True) audit_user_id = fields.IntField(null=True, default=None, readonly=True) `

Usage: ` from tortoise.contrib.pydantic import pydantic_model_creator

schema = pydantic_model_creator(Channel, exclude_readonly=True) `

Error:

Despite exclude_readonly=True, create_user_id and audit_user_id are included in the schema and they are not marked as optional.

abondar commented 1 month ago

Not sure what you are trying to do here

There is no param readonly for model fields

If you want to exclude fields from schema - try using exclude param

zhaowanpeng commented 1 month ago

Thank you for your reply. I reviewed the source code again and realized that my understanding was insufficient. Thank you.

It is worth noting that CharField comes with a default_factory by default, whereas IntField does not. This can lead to misunderstandings, causing some to subconsciously assume that CharField and IntField, when having similar parameters, are either both required or both optional.