(Django)如何通过多层关系进行引用? (可能很复杂)

问题描述 投票:0回答:1

以下是用于创意写作应用程序的。所有方面(角色,场景,项目等)都记录在同一模型中,以提供简单的侧边栏目录。每个扩展模型都有添加特定字段的扩展模型,因此,在侧栏中选择元素时,扩展模型将作为右侧的表单加载。 为清晰起见,排除了扩展模型附加字段和Universe模型。

问题是,我真的很想限制特定的ManytoMany字段上的选择。

MODELS.PY

user_information <only 1 record>
    current_universe <will be changed through a signal depending on the universe loaded>

elements_type 
    name <e.g. ID 1 = character, ID 2 = project, ID 3 = scene, ID 4 = draft>

elements
    name = models.CharField(max_length=100)
    elements_type = models.ForeignKey(elements_type, on_delete=models.CASCADE)
    universe = models.PositiveSmallIntegerField() <default value will be set automatically to match current_universe>
    parent_ID models.ForeignKey('self', on_delete=models.CASCADE) <if the element is a scene, this will identify its draft; likewise with a draft and its project>

extension_projects
    name
    elements_id = models.ForeignKey(elements, on_delete=models.CASCADE)
    characters = models.ManyToManyField(elements, limit_choices_to={'elements_type': 1, 'universe': ? User_information’s current_universe – record 1}) <don't know how to do this at the moment but that's not part of the question>


extension_drafts
    name
    elements_id

extension_scenes
    name
    elements_id
    characters = models.ManyToManyField(elements, limit_choices_to={'elements_type': 1, < ? elements with the same project as this scene’s 1) element’s 2) parent ID’s 3) parent ID > })

我需要extensions_scenes模型中的字符选择列表,以便仅向我显示与与extension_scene的项目相同的项目相关联的字符。

因此应用程序必须-首先确定该extension_scene记录的项目:

  • 在元素模型中查找与该记录的elements_ID相匹配的ID;]]
  • 对于此找到的元素,找到其ID
  • 与其parent_ID匹配的元素(从而导航到具有草稿作为其类型的元素)
  • 对于此(草稿)元素,找到其ID
  • 与其parent_ID相匹配的元素(因此导航到该草稿元素的项目元素记录)
  • 请注意此元素(项目的)ID
  • -我们称之为X。。

    然后将此字符列表限制为:

  • 仅列出元素(类型为1,即字符);
  • 具有关联的extension_projects记录,其elements_ID
  • 是=到X

    谢谢您的帮助

以下是用于创意写作应用程序的。所有方面(角色,场景,项目等)都记录在同一模型中,以提供简单的侧边栏目录。每个都有要添加的扩展模型...

python django many-to-many
1个回答
0
投票

所以应用程序必须-首先确定该extension_scene记录的项目:

© www.soinside.com 2019 - 2024. All rights reserved.