简体   繁体   中英

hybrid_property raises a TypeError in if/else

I'd like to do some formatting on the id in a hybrid property so that numbers under 10 are prefixed with a P0 (P01, P02, 03, etc). The following code is throwing a TypeError: Boolean value of this clause is not defined from the __bool__ method in elements.py. What am I missing?

In my models.py:

@hybrid_property
def conversion_number(self):
    return 'P0{}'.format(self.id) if self.id < 10 else 'P{}'.format(self.id)

This was my fix:

@property
def conversion_number(self):
    return 'P0{}'.format(self.id) if self.id < 10 else 'P{}'.format(self.id)

The issue seemed to be that id is an InstrumentedAttribute (not an int) when sqlalchemy is initializing the model. This behavior is strange because my implementation isn't substantially different from the example in the SQLAlchemy docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM