我想給一個分類,以每一個配方,我需要3個級別( - >類 - >子類主題)模型。我有以下型號:Django的 - 有兩個以上的外鍵
class Recipe(models.Model):
content = HTMLField()
...
class Subject(models.Model):
name = models.CharField()
class Category(models.Model):
subject = models.ForeignKey(Subject)
name = models.CharField()
class Subcategory(models.Model):
category = models.ForeignKey(Category)
name = models.CharField()
起初,我當時就想,一個ForeignKey添加到配方模型,是這樣的:
subcategory = models.ForeignKey(Subcategory)
所以我能夠從子類去主題,做一個簡單的加入。
但也許是更好地創建一個新表,以提高性能並有一個可擴展的系統,這是我的想法:
class Classification(models.Model):
recipe = models.ForeignKey(Recipe)
subject = models.ForeignKey(Subject)
category = models.ForeignKey(Category)
subcategory = models.ForeignKey(Subcategory)
利用這最後的解決方案,我想選擇從配方管理的子類別面板,然後自動填寫其他字段(類別和主題)。我的問題是:我該怎麼做?
謝謝!