Python peewee:在模型类中为self属性引用self值

时间:2017-07-20 22:30:48

标签: python model peewee

我上课了:

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)是一个整数值,但它返回一个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)

如何引用属性的实际值(在这种情况下为整数)?

1 个答案:

答案 0 :(得分:0)

作为类属性访问时,将使用字段本身。您需要将其作为实例属性进行访问,以便检索self.exp_day

的值