私はdjangoアプリケーションを作成していますが、問題に直面しています。ForeignKey
あるモデルが別のモデルに依存するモデルを定義しようとしていますForeignKey
。
説明部分
私のアプリケーションは選択に関するものです。
したがって、decision
作成する があり、adecision
には複数choice
の があり、achoice
にはstatus
(他の制約があるため) があるとします。
status
は複数の で使用できますがchoices
、は がリンクされている同じ 1 つの にstatus
のみ関連できます。decision
choice
データベース スキーマ
修正されておらず、必要に応じて変更される可能性があります。
,____________, ,____________,
| | 1, n 1, 1 | |
| Decision |------------------| Status |
|____________| |____________|
| |
| 1, n | 1, n
| |
| 1, 1 |
,_____|______, |
| | 1, 1 |
| Choice |-------------------------'
|____________|
コード
そして、ここに私の現在の(簡略化された)(動作していない)コードがあります:
class Decision (models.Model):
name = models.CharField (max_length = 63)
class Status (models.Model):
value = models.CharField (max_length = 63)
decision = models.ForeignKey (Decision)
class Choice (models.Model):
name = models.CharField (max_length = 63)
decision = models.ForeignKey (Decision)
status = models.ForeignKey (Status, limit_choices_to = {'decision' : decision})
ここで重要な部分はlimit_choices_to = {'decision' : decision}
.
追加情報
同じ質問を扱っている別の SO 質問 ( In django, how to limit Choices of a foreignfield based on another field in the same model? ) を見つけましたが、質問は古くなり、最良の答えは外部アプリに依存していました ( django-smart-selects )。
外部のものを使用する必要はなく、3 テーブルの関係のような単純なものを Django だけでは解決できない理由がわかりません。
誰かが解決策、または提案を持っている場合は、教えてください。