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

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

Closed
TLipede opened this issue Dec 22, 2020 · 3 comments · Fixed by #1741
Closed

Comments

@TLipede
Copy link

TLipede commented Dec 22, 2020

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.

@jbertman
Copy link

+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

@boh5
Copy link

boh5 commented May 31, 2021

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)

@spwoodcock
Copy link

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.

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

Successfully merging a pull request may close this issue.

4 participants