简体   繁体   中英

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;

  • Under project there are product_config models, primary key=id;
    • Under each product_config there is a pre_config model, primary key=product_id;
      • Under each pre_config there is a list of sub_config models, primary key=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.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. In a shell I can query the list with:

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?

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 .

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 .)

You can then iterate over assemblies to get the sub config of each config_assembly , like so: 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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