是否可以有一个外键引用 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,null=True)

我的问题是在 account_from/account_to 字段

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

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

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

附言我已经检查了几个会计 github 存储库,但基本上它们没有实施辅助分类帐帐户。它们都是总帐科目。单独的 InventoryAccount 类是我尝试实现辅助分类帐的尝试。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...