Python Django计算模型中所有对象的复合利息

Python Django计算模型中所有对象的复合利息,python,django,Python,Django,使用Python 3.8.1和Django 3.0.1 我正忙于一个财务应用程序来跟踪未偿还的借项并计算未偿还余额的利息 我有三种型号: 债务人-包含客户的个人信息以及 适用利率、复利期等 债务人账户- 在创建新客户端时创建。它还保持了一个运行状态 余额和应计利息 债务人交易–记录交易类型 交易描述、借方/贷方、金额等 型号.py class Debtor(models.Model): name = models.CharField(max_length=200, default="

使用Python 3.8.1和Django 3.0.1

我正忙于一个财务应用程序来跟踪未偿还的借项并计算未偿还余额的利息

我有三种型号:

  • 债务人-包含客户的个人信息以及 适用利率、复利期等
  • 债务人账户- 在创建新客户端时创建。它还保持了一个运行状态 余额和应计利息
  • 债务人交易–记录交易类型 交易描述、借方/贷方、金额等
型号.py

class Debtor(models.Model):
    name = models.CharField(max_length=200, default="")
    period = models.DecimalField(max_digits=100, decimal_places=2, default=0, null=True)
    interestrate = models.DecimalField(max_digits=100, decimal_places=2, default=0, null=True, blank=True)

    def __str__(self):
        return self.name


@property
def unique_id(self):
    return str(self.pk)


class DebtorAccount(models.Model):
    accountname = models.OneToOneField(Debtor, on_delete=models.CASCADE)
    balance = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    interest = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    rate = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    period = models.DecimalField(max_digits=100, decimal_places=2, default=0)

    def __str__(self):
        return str(self.accountname)


def create_debtoraccount(sender, **kwargs):
    if kwargs['created']:
        debtor_account = DebtorAccount.objects.create(accountname=kwargs['instance'])


post_save.connect(create_debtoraccount, sender=Debtor)


class DebtorTransaction(models.Model):
    CREDIT = 'CREDIT'
    DEBIT = 'DEBIT'
    TRANSACTION_TYPES = (
        ("CREDIT", "Credit"),
        ("DEBIT", "Debit"),
    )

    debtor = models.ForeignKey(DebtorAccount, on_delete=models.CASCADE, blank=True, null=True)
    amount = models.DecimalField(max_digits=100, decimal_places=2, default=0)
    description = models.CharField(max_length=200, blank=True, default="Transaction Description")
    date = models.DateTimeField(auto_now_add=True)
    type = models.CharField(choices=TRANSACTION_TYPES, max_length=6)
    transaction_balance = models.DecimalField(max_digits=100, decimal_places=2, default=0, null=True, blank=True)

    def __str__(self):
        return str(self.debtor)


    @property
    def calculate_interest(user_account):
        user_account = DebtorAccount.objects.all()
        for account in user_account:
            user_interest = (account.balance * (1 + (account.rate / account.period))) - account.balance
            account.save()
            return user_interest

    @property
    def get_transaction_balance(self):
        user_account = DebtorAccount.objects.get(accountname=self.debtor.id)
        if self.type == DebtorTransaction.DEBIT:
            user_balance = user_account.balance + self.amount + self.initiationfee
        else:
            user_balance = user_account.balance - self.amount
        return user_balance


    def save(self, *args, **kwargs):
        self.interest = self.calculate_interest
        self.transaction_balance = self.get_transaction_balance
        super(DebtorTransaction, self).save(*args, **kwargs)


@receiver(post_save, sender=DebtorTransaction, dispatch_uid="transaction log entry")
def update_user_account(sender, instance, **kwargs):
    user_account = DebtorAccount.objects.get(accountname=instance.debtor.id)
    if instance.type == DebtorTransaction.CREDIT:
        user_account.balance = F('balance') - instance.amount
    else:
        user_account.balance = F('balance') + instance.amount

    user_account.save()

表格示例

我使用Django表2来显示客户机和事务。我不知道是否有必要使用get_transaction_balance函数,但这是我能够在事务表中使用事务的运行平衡的唯一方法

我现在要计算每个客户未偿余额的复利。我希望在每个客户每月的第一天自动计算。据我所知,芹菜是实现这一目标的最佳方法,但我以前从未使用过,也尚未开始研究

现在我只想计算每个客户未付余额的利息。稍后,我还将改进利息公式,以针对不同客户的不同复利期进行调整

我完全被困在如何做到这一点上。calculate_利息函数是我最近尝试解决这个问题的一次尝试,但它不起作用

利息仅根据最后一个客户的未偿余额计算,然后应用于特定实例。它不是为每个客户端单独计算并保存的

我知道问题出在for循环和save方法被覆盖时。我只是不知道怎么解决它


如何计算每个客户未付余额的利息?

您当前的问题是,您只计算第一个债务人账户的利息,然后返回。然而,我真的不明白为什么这是一个正在进行计算的属性。这应该是一种方法,以便用户知道数据正在更改。属性更改数据是不常见的

@property
def calculate_interest(self): # Use the self argument for properties.
    user_accounts = DebtorAccount.objects.all()
    for account in user_accounts:
        # No need for both the subtraction and the 1 + portion.
        user_interest = account.balance * (account.rate / account.period)
        account.save()
    return # I don't know what you want to return from this
另外,我真的不知道为什么单个DebtorTransaction实例上的属性会对完全无关的DebtorAccount实例执行更改。也许你的意思是:

def calculate_interest(self):
    account = self.debtor
    # No need for both the subtraction and the 1 + portion.
    user_interest = account.balance * (account.rate / account.period)
    account.interest = user_interest
    account.save()
    return user_interest

尽管如此,由于我对您的场景缺乏了解,我仍然不能确定您的数据模型是否正确。

谢谢您的帮助。我误解了物业装饰师的意思,并按照你的建议修改了我的代码。一切都很顺利。非常感谢。