django - 是否可以有一个外键引用 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 类是我实现明细账的尝试。
解决方案
推荐阅读
- javascript - 在页面加载时自动按下特定按钮的方法?
- sql-server - 在 SQL 递归中使用 MAX(Date) 值
- reactjs - React Navigation 在 contentComponent 中显示选定的屏幕
- java - 没有花括号的 If-Else 控制流
- ruby - 什么是在 ruby 中对字符串进行排序以使空字符串位于末尾的好方法?
- jenkins - Jenkins 多分支管道缺少一个分支
- angular - Rxjs 接缝无法找到它的一些成员
- bootstrap-4 - 是否可以在标题属性(data-html =“true”)Bootstrap 4 Tooltip的标签内添加类?
- javascript - Puppeteer - 如何使用 XPath 获取标签的长度?
- sql - 如何使用 Select Query 更新前 (20) 条记录