鑑於這些模型,我如何防止將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()
如果他們是不同類型的東西,那麼爲什麼不能在一個模型的字段例如識別不同的事情鍵入字段 –
它們是具有許多不同字段的完全不同類型的東西。 –