简体   繁体   English

Django:查询外键的外键

[英]Django: query of a foreign key of a foreign key

This might be stupid question and I am sure that there is a basic query for this situation, but I don't seem to get a hang of it and Google turned out to be a miss for solution.这可能是一个愚蠢的问题,我确信对于这种情况有一个基本的查询,但我似乎没有掌握它,谷歌原来是一个解决方案的错过。

I have models:我有模型:

Project, primary key=project_no;项目,主键=project_no;

  • Under project there are product_config models, primary key=id;项目下有product_config模型,主键=id;
    • Under each product_config there is a pre_config model, primary key=product_id;每个product_config下都有一个pre_config模型,primary key=product_id;
      • Under each pre_config there is a list of sub_config models, primary key=id;每个 pre_config 下都有一个 sub_config 模型列表,主键=id;

Now I am loading a page for project details and I pass a project_no and make a query for all product_configs:现在我正在加载一个项目详细信息页面,并传递一个 project_no 并查询所有 product_configs:

project.objects.get(project_no=project_no)

product_config.objects.filter(project_no=project_no)

Now I want to create a table for the list of sub_configs according to pre_config under product_config.现在我想根据product_config下的pre_config为sub_configs列表创建一个表。 In a shell I can query the list with:在 shell 中,我可以使用以下命令查询列表:

config_assembly.objects.filter(product_id=product_id)

How I can pass the product_id of pre_config from my product_config to query all the sub_configs?如何从我的 product_config 传递 pre_config 的 product_id 来查询所有 sub_configs?

EDIT:编辑:

This is the basic structure of my models.这是我的模型的基本结构。

in project.models

class project(models.Model):
    project_no = IntegerField('project no', primary_key=True)

class product_config(models.Model):
    project_no = models.ForeignKey('project.project', on_delete=models.CASCADE, verbose_name='project no')
    product_id = models.ForeignKey('product.pre_config', on_delete=models.CASCADE, verbose_name='product code')



in product.models

class pre_config(models.Model):
    product_id = models.CharField('product code', max_length=30, unique=True, primary_key=True)

class sub_config(models.Model):
    subproduct_id = models.CharField('subproduct code', max_length=20, unique=True, primary_key=True)



in assembly.models

class config_assembly(models.Model):
    product_id = models.ForeignKey('product.pre_config', on_delete=models.CASCADE, verbose_name='product code')
    subconfig_id = models.ForeignKey('product.sub_config', on_delete=models.CASCADE, verbose_name='subproduct code')


For your use case it sounds like you want to use the API for related objects .对于您的用例,听起来您想将 API 用于相关对象

If I'm understanding your question correctly, here's an example:如果我正确理解你的问题,这里有一个例子:

# Of course, you should handle cases when 0 or multiple product_configs
# match the query, and replace QUERY with an actual selector.

# This is the related pre_config's product_id
my_pre_config = product_config.objects.get(QUERY).product_id

assemblies = my_pre_config.config_assembly_set.all()

(I follow this standard for class names, so I may be mistaken about _set in your case. It could be configassembly_set .) (我遵循这个类名标准,所以在你的情况下我可能会误认为_set 。它可能是configassembly_set 。)

You can then iterate over assemblies to get the sub config of each config_assembly , like so: assembly.subconfig_id .然后,您可以遍历assemblies以获取每个config_assembly的子配置,如下所示: assembly.subconfig_id


Also, for the purposes of readability, I would recommend renaming your foreign key fields from foo_id to something signifying the actual model that the foreign key points to.此外,为了便于阅读,我建议将您的外键字段从foo_id重命名为表示外键指向的实际模型的名称。 For example:例如:

# models.py
class product_config(models.Model):
    project = models.ForeignKey('project.project', on_delete=models.CASCADE)

Even though the database values will be IDs, in your code you probably want to reference my_product_config.project.project_no instead of my_product_config.project_no.project_no , which can get confusing and lead to mistaking model instances for raw ID values, and vice-versa.即使数据库值将是 ID,在您的代码中,您可能希望引用my_product_config.project.project_no而不是my_product_config.project_no.project_no ,这可能会导致混淆并导致将模型实例误认为是原始 ID 值,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM