是否有可能根据django中的情况让一个模型引用很多其他模型?
Is it possible to have a model reference many other models depending on the situation in django?
老实说,我什至不知道如何表达我的问题,但我认为举个例子可能会有所帮助。
恭敬地狂刷了三个自带模型的app。应用程序 1、应用程序 2、应用程序 3。我想创建一个新应用程序(报告),它将报告我的每个其他应用程序(App1、App2、App3)。根据我目前的知识,我的报告中包含以下内容 models.py
class Reports(models.Model):
name = models.CharField(max_length=150, blank=True)
app1_reported = models.ForeignKey(
App1, on_delete=models.CASCADE, related_name='reported_app1', blank=True)
app2_reported = models.ForeignKey(
App2, on_delete=models.CASCADE, related_name='reported_app2', blank=True)
app3_reported = models.ForeignKey(
App3, on_delete=models.CASCADE, related_name='reported_app3', blank=True)
我有什么方法可以在我的报告模型中创建一个可以引用 App1、App2 或 App3 的引用吗?类似于:
class Reports(models.Model):
name = models.CharField(max_length=150, blank=True)
reported = models.ForeignKey(
App1 or App2 or App3, on_delete=models.CASCADE, related_name='reported_app_is', blank=True)
我的应用程序在第一个实现中运行良好我只是想知道是否有更好的方法。另外请让我知道这是否有意义,我正在努力寻找正确的措辞。
您可以使用内容类型框架。这里有一个link供大家参考
老实说,我什至不知道如何表达我的问题,但我认为举个例子可能会有所帮助。
恭敬地狂刷了三个自带模型的app。应用程序 1、应用程序 2、应用程序 3。我想创建一个新应用程序(报告),它将报告我的每个其他应用程序(App1、App2、App3)。根据我目前的知识,我的报告中包含以下内容 models.py
class Reports(models.Model):
name = models.CharField(max_length=150, blank=True)
app1_reported = models.ForeignKey(
App1, on_delete=models.CASCADE, related_name='reported_app1', blank=True)
app2_reported = models.ForeignKey(
App2, on_delete=models.CASCADE, related_name='reported_app2', blank=True)
app3_reported = models.ForeignKey(
App3, on_delete=models.CASCADE, related_name='reported_app3', blank=True)
我有什么方法可以在我的报告模型中创建一个可以引用 App1、App2 或 App3 的引用吗?类似于:
class Reports(models.Model):
name = models.CharField(max_length=150, blank=True)
reported = models.ForeignKey(
App1 or App2 or App3, on_delete=models.CASCADE, related_name='reported_app_is', blank=True)
我的应用程序在第一个实现中运行良好我只是想知道是否有更好的方法。另外请让我知道这是否有意义,我正在努力寻找正确的措辞。
您可以使用内容类型框架。这里有一个link供大家参考