3

私はdjangoアプリケーションを作成していますが、問題に直面しています。ForeignKeyあるモデルが別のモデルに依存するモデルを定義しようとしていますForeignKey

説明部分

私のアプリケーションは選択に関するものです。

したがって、decision作成する があり、adecisionには複数choiceの があり、achoiceにはstatus(他の制約があるため) があるとします。

statusは複数の で使用できますがchoices、は がリンクされている同じ 1 つの にstatusのみ関連できます。decisionchoice

データベース スキーマ

修正されておらず、必要に応じて変更される可能性があります。

,____________,                  ,____________,
|            | 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 だけでは解決できない理由がわかりません。

誰かが解決策、または提案を持っている場合は、教えてください。

4

4 に答える 4

1

throughここで必要なのは、次のようなモデルだと思います。

class Choice (models.Model):
    name = models.CharField (max_length = 63)
    status = models.ForeignKey(Status)
    decision = models.ForeignKey(Decision)

class Status(Models.Model):
    name = models.CharField(max_length=20)

class Decision(models.Model):
    name = models.CharField(max_length = 63)
    choices = models.ManyToManyField(Status, through = "Choice")    

このように、すべての決定には多くの選択肢があり、それぞれのステータスは 1 つだけです。次のようなクエリを実行できます: my_decision.choices.all()またはmy_status.decision_set.all()

モデルを使用する方法の例については、ドキュメントをご覧になることをお勧めします

于 2013-07-08T22:46:10.070 に答える
0

次のステートメントdecisionでは、呼び出し可能でもmodels.Qオブジェクトでもありません。

status = models.ForeignKey (Status, limit_choices_to = {'decision' : decision})

データを表す方法は次のとおりです。

class Decision(models.Model):
    ...

# a status is relevant for only one decision
# there may be more than one Status per Decision.
class Status(Models.Model):
    decision = models.ForeignKey(Decision)

# each choice is linked to one decision and has a status.
class Choice (models.Model):
    status = models.ForeignKey(Status)
    # if status is mandatory, then you can get the decision
    # from status.decision. per se, this fk could be optional.
    decision = models.ForeignKey(Decision)

ここに別のものがあります:

class Decision(models.Model):
    ...

# a decision has multiple choices
# a choice pertains to only one decision
class Choice (models.Model):
    decision = models.ForeignKey(Decision)

# each status is relevant to one decision
# and may encompass multiple choices.
class Status(Models.Model):
    decision = models.ForeignKey(Decision)
    # problem with this representation is that this allows for
    # a choice to be linked to multiple statuses.
    # this happens when using M2M instead of ForeignKey.
    choices = models.ManyToManyField(Choice)
于 2013-07-09T09:52:58.150 に答える