Search code examples
pythondictionarypeewee

Peewee model to dict doesn't include hybrid property


This is my peewee model

class OAuthAccount(BaseModel):
    id = BigIntegerField(primary_key=True,unique=True ,null = False, db_column="id")
    oauth_provider_id = IntegerField(null=False)
    oauth_uid = CharField()
    oauth_token = CharField()
    oauth_token_secret = CharField()
    username = CharField()
    inserter = BigIntegerField(null=True,db_column="inserter_id")
    insert_date = DateTimeField(null=True,default=fn.NOW())
    updater = BigIntegerField(null=True,db_column="updater_id")
    update_date = DateTimeField(null=True)
    extra_data = CharField()

    @hybrid_property
    def oauth_provider_name(self):
        return OAuthProviderEnum.getByValue(self.oauth_provider_id).label

When I convert model to dict model_to_dict(OAuthAccount,row) it doesn't include hybrid_property oauth_provider_name.

{
    id
    oauth_provider_id
    oauth_uid
    oauth_token
    oauth_token_secret
    username
    inserter
    insert_date
    updater
    update_date
    extra_data
}

Is it possible include @hybrid_property in dict when use model_to_dict?


Solution

  • I will add an optional param called "extra" that allows you to specify things like model methods, attributes, etc.