0

这是我的理解,这应该工作......

   def _balance(self):
        aggregates = self.transactions.aggregate(sum=Sum('amount'))
        sum = aggregates['sum']
        return D('0') if sum is None else sum

但我收到以下错误:“帐户”对象没有属性“交易”

我在模型事务上设置了相关名称事务。在下面查看我的模型

class Account(models.Model):
    OPEN, FROZEN, CLOSED = 'Open', 'Frozen', 'Closed'
    status = models.CharField(max_length=32, default=OPEN)
    # For performance, we keep a cached balance
    balance = models.DecimalField(decimal_places=5, max_digits=10,
                                  default=D('0'), null=True)
    #FK
    user = models.OneToOneField(User)

    def _balance(self):
            aggregates = self.transactions.aggregate(sum=Sum('amount'))
            sum = aggregates['sum']
            return D('0') if sum is None else sum


class Transaction(models.Model):
    account = models.ForeignKey('Account',
                                related_name='transactions')

知道为什么我的模型功能不起作用吗?

谢谢你。

4

0 回答 0