如何在Django中实现交易和帐户系统?

我正在构建一个存储交易和帐户的个人理财应用程序。每笔交易都会影响帐户余额。如何在我的应用程序中实现呢?尽管搜索了一段时间我没有找到具体的答案。

models.py

class Account(models.Model):
    DateCreated = models.DateTimeField()
    AccountName = models.SlugField(max_length= 100, primary_key=True)
    UserID = models.ForeignKey(MyUser, on_delete=models.CASCADE)      
    Type = models.CharField(max_length= 20)
    Balance = models.DecimalField(max_digits=19, decimal_places=8)
    Value = models.DecimalField(max_digits=19, decimal_places=8)

class Transaction(models.Model):

    TypeChoices = (
                    ('Income', 'Income'),
                    ('Expense', 'Expense'),
                    ('Transfer', 'Transfer'),
                    )

    CategoryChoices = (
                        ('Rent', 'Rent'),
                        ('Utilities', 'Utilities'),
                        ('Food', 'Food'),
                        ('Leisure', 'Leisure'),
                        ('Insurance', 'Insurance'),
                        ('Gas', 'Gas'),
                        ('Savings', 'Savings'),
                        ('Investment', 'Investment'),
                        ('Phone Payment', 'Phone Payment'),
                        ('Gym Payment','Gym Payment'),
                        ('Salary', 'Salary'),
                        ('Asset', 'Asset'),
                        ('Miscellaneous', 'Miscellaneous'),
                        ('Transfer', 'Transfer'),
                        )

    AccountName = models.ForeignKey(Account, on_delete=models.CASCADE)
    UserID = models.ForeignKey(MyUser, on_delete=models.CASCADE)
    Date = models.DateField()
    Type = models.CharField(max_length = 8, choices = TypeChoices)
    Entity = models.CharField(max_length = 100)
    Description = models.CharField(max_length = 200)
    BudgetType = models.CharField(max_length = 20, choices = CategoryChoices)
    Amount = models.DecimalField(max_digits=19, decimal_places=8)

我目前已经创建了视图,该视图将允许用户创建,更新和删除交易和帐户,但没有影响帐户余额的交易。实现此目的的最佳方法是什么?

评论