鉴于这些模型,我如何防止将FinancialTransaction分配给多个Thing?如何将Django模型限制为几种可能的关系之一?
换句话说,如果ThingOne有一个FinancialTransaction,ThingTwo或ThingThree不能与它有关系。
如何在管理员中执行此操作?我当然可以通过Inline在SomeThing管理员中获得Thing *,但是这允许我设置多个Thing *。
我的第一个倾向是我的模型是错误的,所有的东西都应该用一个模型来表示,但它们绝对是不同类型的东西。
from django.db import models
class ThingOne(models.Model):
name = models.CharField(max_length=20)
some_things = models.ForeignKey('FinancialTransaction', blank = True, null = True)
class ThingTwo(models.Model):
name = models.CharField(max_length=20)
some_things = models.ForeignKey('FinancialTransaction', blank = True, null = True)
thingone = models.ForeignKey(ThingOne)
class ThingThree(models.Model):
name = models.CharField(max_length=20)
some_things = models.ForeignKey('FinancialTransaction', blank = True, null = True)
thingtwo = models.ForeignKey(ThingTwo)
class FinancialTransaction(models.Model):
value = models.IntegerField()
如果他们是不同类型的东西,那么为什么不能在一个模型的字段例如识别不同的事情键入字段 –
它们是具有许多不同字段的完全不同类型的东西。 –