首页 > 解决方案 > 是否可以有一个外键引用 Django 中的多个类?

问题描述

我正在为一个会计系统建模。我有以下型号:

class GeneralAccount(MPTTModel):
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
    name = models.CharField(max_length=100, verbose_name='Account Name')
    code = models.CharField(max_length=10, verbose_name='Account Code')
    balance = models.DecimalField(max_digits=9, decimal_places=2)

class InventoryAccount(GeneralAccount): 
    description = models.TextField()
    price = models.DecimalField(max_digits=9, decimal_places=2)
    cost = models.DecimalField(max_digits=9, decimal_places=2)
    available_stock = models.DecimalField(max_digits=9, decimal_places=2)
    value =  models.DecimalField(max_digits=9, decimal_places=2)    


class JournalEntry(models.Model):
    transaction_date = models.DateField(verbose_name='Transaction Date', default=django.utils.timezone.now)
    account_from = models.ForeignKey(GeneralAccount, on_delete=models.PROTECT) # THIS IS THE PROBLEM
    account_to = models.ForeignKey(GeneralAccount, on_delete=models.PROTECT) # THIS IS THE PROBLEM
    amount = models.DecimalField(max_digits=9, decimal_places=2, blank=True, null=True)

我的问题出在 account_from/account_to 字段中。

目前,它只接受一个GeneralAccount,但我也希望它接受一个,InventoryAccount因为它本质上是一个MPTTModel/GeneralAccount只有一些专业领域的。

我唯一能想到的就是JournalEntry为 Inventory 创建一个单独的类,但我更愿意简化它。实际上,如果我只能拥有一种 MPTT 模型(即一个帐户模型),那将是可取的。

有没有办法解决这个问题?

PS 我已经检查了几个会计 github 存储库,但基本上他们没有实施附属分类帐帐户。它们都是总分类帐帐户。单独的 InventoryAccount 类是我实现明细账的尝试。

标签: djangoaccounting

解决方案


推荐阅读