(Django) How to reference through multiple layers of relationships?

别来无恙 提交于 2020-07-23 06:39:06

问题


The following is for a creative writing app. All aspects (characters, scenes, projects etc.) are recorded in the same model to allow for a simple sidebar directory. Each have extension models to add specific fields, so when an element is selected in the sidebar, the extension model is loaded as a form on the right. Extension model additional fields and universe model excluded for clarity.

The problem is, I'm really stuck with restricting choices on a specific ManytoMany field.

MODELS.PY

class user_information
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    current_universe = models.PositiveSmallIntegerField(blank=True, null=True)

class element_type 
    name <e.g. ID 1 = universe, ID 2 = project, ID 3 = scene, ID 4 = draft, ID 5 = characters>

class element
    name = models.CharField(max_length=100)
    elements_type = models.ForeignKey(element_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>

class extension_project
    name = models.CharField(max_length=100)
    element_id = models.ForeignKey(element, on_delete=models.CASCADE)
    characters = models.ManyToManyField(element, 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>


class extension_draft
    name = models.CharField(max_length=100)
    element_id = models.ForeignKey(element, on_delete=models.CASCADE)

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

I need the characters selection list in this extensions_scenes model, to only show me characters who are associated with an existing project (a connection made through the extension_project model with the characters column). How does it know which project? By matching the project of the currently selected extension_scene record with the element_id column of the extension_project model. Which project the extensions_scene belongs to can be found in the element model.

So the app has to - firstly identify this extension_scene record's project:

  • look in the elements model for an ID that matches this record's elements_ID;
  • for this found element, find the element whose ID matches its parent_ID (thus navigating to an element that has draft as it's type)
  • for this (draft) element, find the element whose ID matches its parent_ID (thus navigating to this draft element's project element record)
  • take note of this element's (the project's) ID - let's call this X. .

Then restrict this character list to:

  • only list elements (with a type of 1, i.e. characters);
  • search the extension_projects record, and find an elements_ID = to X

Thank you for any help


回答1:


So the app has to - firstly identify this extension_scene record's project:

  • look in the elements model for an ID that matches this record's elements_ID;
  • for this found element, find the element whose ID matches its parent_ID (thus navigating to an element that has draft as it's type)
  • for this (draft) element, find the element whose ID matches its parent_ID (thus navigating to this draft element's project element record)
  • take note of this element's (the project's) ID - let's call this X. .

As for the above part, extension_scenes.objects.all().values('elements_id__parent_ID__parent_ID')
This can be the solution as far as I understood.

But I couldn't understand the requirement of later part.



来源:https://stackoverflow.com/questions/61700583/django-how-to-reference-through-multiple-layers-of-relationships

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!