Python peewee:在混合模型 class 中引用自我价值 属性
Python peewee: refer self value in a model class for hybrid property
我有一个 class:
class FixedTransaction(Transaction):
except_dates = JSONField(null=True)
active = BooleanField(null=True)
exp_day = IntegerField()
_exp_date_month = None
_exp_date_year = None
@hybrid_property
def is_exp(self):
return datetime(2018, 5, self.exp_day)
我需要获取 属性 (exp_day) 的值,它是一个整数值,但它 returns 是 IntergerField:
的一个实例
Traceback (most recent call last):
File "/home/ginoepri/Development/Python/pkg/console.py", line 85, in <module>
fixeds = FixedTransaction.select().where(FixedTransaction.is_exp > datetime(2010, 5, 1))
File "/usr/lib/python3.6/site-packages/playhouse/hybrid.py", line 27, in __get__
return self.expr(instance_type)
File "/home/ginoepri/Development/Python/pkg/models/fixed_transaction.py", line 20, in is_exp
return datetime(2018, 5, self.exp_day)
TypeError: an integer is required (got type IntegerField)
如何引用属性的实际值(在本例中为整数)?
当作为 class 属性访问时,将使用字段本身。您需要将其作为实例属性访问,以便检索 self.exp_day
.
的值
我有一个 class:
class FixedTransaction(Transaction):
except_dates = JSONField(null=True)
active = BooleanField(null=True)
exp_day = IntegerField()
_exp_date_month = None
_exp_date_year = None
@hybrid_property
def is_exp(self):
return datetime(2018, 5, self.exp_day)
我需要获取 属性 (exp_day) 的值,它是一个整数值,但它 returns 是 IntergerField:
的一个实例Traceback (most recent call last):
File "/home/ginoepri/Development/Python/pkg/console.py", line 85, in <module>
fixeds = FixedTransaction.select().where(FixedTransaction.is_exp > datetime(2010, 5, 1))
File "/usr/lib/python3.6/site-packages/playhouse/hybrid.py", line 27, in __get__
return self.expr(instance_type)
File "/home/ginoepri/Development/Python/pkg/models/fixed_transaction.py", line 20, in is_exp
return datetime(2018, 5, self.exp_day)
TypeError: an integer is required (got type IntegerField)
如何引用属性的实际值(在本例中为整数)?
当作为 class 属性访问时,将使用字段本身。您需要将其作为实例属性访问,以便检索 self.exp_day
.