简体   繁体   English

如何使用Django ORM查询两个传入模型?

[英]How do I query on two incoming models using the Django ORM?

I've got the following models: 我有以下模型:

class DataStream(models.Model):
    category = models.CharField(max_length=255)
    description = models.TextField()

class DataStreamSubCategory(models.Model):
    data_stream = models.ForeignKey(DataStream)
    sub_category = models.CharField(max_length=255)

class DataStreamExample(models.Model):
    data_stream = models.ForeignKey(DataStream)
    example = models.CharField(max_length=255)

So, each DataStream can have 0 or more DataStreamSubCategory objects and 0 or more DataStreamExample objects. 因此,每个DataStream可以具有0个或多个DataStreamSubCategory对象和0个或多个DataStreamExample对象。

What I want to do is essentially display these values in a simple table in a template: 我要做的基本上是在模板的简单表中显示这些值:

----------------------------------------
| Category | Sub-categories | Examples |
----------------------------------------
| Cat1     | Sub-cat1       | Ex1      |
|          | Sub-cat2       | Ex2      |
|          | Sub-cat3       |          |
----------------------------------------
| Cat2     | Sub-cat4       | Ex1      |
|          |                | Ex2      |
----------------------------------------

So what's the best way to query the database to get these data? 那么查询数据库以获取这些数据的最佳方法是什么? The most obvious (but probably dumb) way is to do this: 最明显(但可能很愚蠢)的方法是:

data_streams = DataStream.objects.all()
for data_stream in data_streams:
    sub_categories = DataStreamSubCategory.objects.filter(data_stream=data_stream)
    examples = DataStreamExample.objects.filter(data_stream=data_stream)

But is there a more efficient way? 但是,有没有更有效的方法? Seems like a few SQL joins should be in order here, but I'm not sure how to accomplish this using the Django ORM. 似乎应该在这里按顺序进行一些SQL连接,但是我不确定如何使用Django ORM完成此操作。

You can query it directly in the template like this: 您可以像这样直接在模板中查询它:

<table>
{% for ds in data_streams %}
   <tr>
      <td>
          {% ds.category %}
      </td>
      <td>
          {% for subcat in ds.datastreamsubsategory_set.all %}
               {{subcat.sub_category}}
          {% endfor %}
      </td>
      <td>
          {% for example in ds.datastreamexample_set.all %}
               {{example.example}}
          {% endfor %}
      </td>
{% empty %}
   <tr><td colspan="3">No categories found</td></tr>
{% endfor %}
</table>

I shall let you figure out the formatting yourself. 我让你自己弄清楚格式。

In the context, just send {'data_streams': data_streams} 在上下文中,只需发送{'data_streams': data_streams}

Basically, for reverse foreign key relationships, you would do object.lowercasemodelname_set.all() to get the related object queryset. 基本上,对于反向外键关系,您可以执行object.lowercasemodelname_set.all()来获取相关的对象queryset。

Read more on lookups that span relationships here 这里阅读更多关于跨越关系的查询

Or you could add a related_name attribute and use that instead of lowercasemodelname_set 或者,您可以添加related_name属性,并使用它代替lowercasemodelname_set

You might want to even consider prefetch_related option if you want to reduce the number of queries on the database 如果要减少数据库中的查询数量,甚至可能要考虑prefetch_related选项

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

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