tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

'exclude_readonly' is ignored when the 'name' argument isn't used in pydantic_model_creator

Open TLipede opened this issue 5 years ago • 3 comments

Describe the bug The pydantic_model_creator function gives different answers when called with the name argument, compared to when it is omitted.

To Reproduce

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


class People(Model):
    id = fields.IntField(pk=True)
    created_at = fields.DatetimeField(auto_now_add=True)
    modified_at = fields.DatetimeField(auto_now=True)

    first_name = fields.CharField(max_length=50)
    last_name = fields.CharField(max_length=50)

# First try
Person = pydantic_model_creator(People)
PersonIn = pydantic_model_creator(People, exclude_readonly=True)

print(list(Person.__fields__))  # [id, created_at, modified_at, first_name_last_name]
print(list(PersonIn.__fields__)) # [id, created_at, modified_at, first_name, last_name]

# Second try
Person = pydantic_model_creator(People, name="Person")
PersonIn = pydantic_model_creator(People, name="PersonIn", exclude_readonly=True)

print(list(Person.__fields__))  # [id, created_at, modified_at, first_name, last_name]
print(list(PersonIn.__fields__))  # [first_name_last_name]

Expected behavior That the same Pydantic classes (the second set) should be generated in both cases.

TLipede avatar Dec 22 '20 22:12 TLipede

+1 here, couldn't figure out what I was doing wrong until I stumbled on this.

My configuration:

base.py

from tortoise.models import Model
from tortoise import fields


class BaseModel(Model):
    id = fields.UUIDField(pk=True)
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)

    class Meta:
        abstract = True

note.py

from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator

from app.db.models.base import BaseModel


class Note(BaseModel):
    message = fields.CharField(max_length=256)


NoteOutput = pydantic_model_creator(
    Note,
    name="Note"
)
NoteInput = pydantic_model_creator(
    Note,
    name="NoteInput",
    exclude_readonly=True
)

Leaving the names off of the pydantic models results in the BaseModel fields erroneously showing up in NoteInput

jbertman avatar Jan 14 '21 20:01 jbertman

I have a same issue。 example model:

class BaseModel(models.Model):
    created_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    updated_at = fields.DatetimeField(auto_now=True, description='修改时间')

    class Meta:
        abstract = True

class Meta(BaseModel):
    id = fields.IntField(pk=True)
    custom_id = fields.CharField(max_length=128, description='自定义ID')
    user: fields.ForeignKeyRelation['User'] = fields.ForeignKeyField('models.User', related_name='metas')

class User(BaseModel):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=128, unique=True, description='名称')
    email = fields.CharField(max_length=256, unique=True, description='邮件地址')
    password_hash = fields.CharField(max_length=128, description='密码哈希')
    metas: fields.ReverseRelation['Meta']

If name argument not used.

__MetaInModel = pydantic_model_creator(Meta,  exclude_readonly=True)

There are 2 problems:

  1. foreignkey object not show in pydantic model
  2. id, created_at, updated_at are required in pydantic model, excluede_readonly not working

if I set name argument, above 2 problems are fixed automatically.

__MetaInModel = pydantic_model_creator(Meta, name='__MetaInModel', exclude_readonly=True)

boh5 avatar May 31 '21 03:05 boh5

Also receiving this error, lucky I stumbled upon this issue. It would be great if #735 could be reviewed and merged. It seems like the only issues preventing a merge is docstring formatting.

spwoodcock avatar May 14 '22 10:05 spwoodcock