tortoise-orm
                                
                                 tortoise-orm copied to clipboard
                                
                                    tortoise-orm copied to clipboard
                            
                            
                            
                        How can I add a virtual property to Model? and it can be set in controller.
I'm using fastapi and Tortoise ORM, I need add an no-database property to a model like this:
class Account(models.Model):
    id = fields.IntField(pk=True)
    email = fields.CharField(max_length=64)
    is_new = 0 # this is the abstract property will be set other value in service.
Account_Pydantic = pydantic_model_creator(Account, name="Account")
but this will not have property is_new in the schema, and when I generate open-api dart codes, it will come errors. Can anyone helps? Thanks a lot.
I could be wrong, but I think it is currently not possible to do this as you would like to do it. As a workaround you could create a customised model with pydantic that contains the property you want in the schema
from tortoise import fields, models
from pydantic import BaseModel, ConfigDict, Field
class Account(models.Model):
    id = fields.IntField(pk=True)
    email = fields.CharField(max_length=64)
    def dict(self):
        return self.__dict__
class AccountWithIsNew(BaseModel):
    id: int
    email: str
    is_new: Optional[int] = Field(default=0)
    model_config = ConfigDict(from_attributes=True)
acc = await Account.create(email="test")
acc_with_is_new = AccountWithIsNew(**acc.dict())
print(acc_with_is_new)
ouput
id=1 email='test' is_new=0