如何减少数据库查询?

发布于 2024-10-19 10:42:01 字数 948 浏览 1 评论 0原文

模型:

class Technology(models.Model):
    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)

class Site(models.Model):
    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    technology = models.ManyToManyField(Technology, blank=True, null=True)

视图:

def portfolio(request, page=1):
    sites_list = Site.objects.select_related('technology').only('technology__name', 'name', 'slug',)
    return render_to_response('portfolio.html', {'sites':sites_list,}, context_instance=RequestContext(request))

模板:

{% for site in sites %}
<div>
    {{ site.name }},
    {% for tech in site.technology.all %}
        {{ tech.name }}
    {% endfor %}
</div>
{% endfor %}

但在该示例中,每个站点都会进行 1 个附加查询来获取技术列表。有没有办法以某种方式在 1 个查询中完成它?

Models:

class Technology(models.Model):
    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)

class Site(models.Model):
    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    technology = models.ManyToManyField(Technology, blank=True, null=True)

Views:

def portfolio(request, page=1):
    sites_list = Site.objects.select_related('technology').only('technology__name', 'name', 'slug',)
    return render_to_response('portfolio.html', {'sites':sites_list,}, context_instance=RequestContext(request))

Template:

{% for site in sites %}
<div>
    {{ site.name }},
    {% for tech in site.technology.all %}
        {{ tech.name }}
    {% endfor %}
</div>
{% endfor %}

But in that example each site makes 1 additional query to get technology list. Is there any way to make it in 1 query somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初吻给了烟 2024-10-26 10:42:01

您正在寻找的是一种进行反向外键查找的有效方法。通用方法是:

qs = MyRelatedObject.objects.all()
obj_dict = dict([(obj.id, obj) for obj in qs])
objects = MyObject.objects.filter(myrelatedobj__in=qs)
relation_dict = {}
for obj in objects:
    relation_dict.setdefault(obj.myobject_id, []).append(obj)
for id, related_items in relation_dict.items():
    obj_dict[id].related_items = related_items

我不久前写了一篇关于此的博文,您可以在这里找到更多信息:http://bit.ly /ge59D2

What you are looking for is an efficient way to do reverse foreign-key lookups. A generic approach is:

qs = MyRelatedObject.objects.all()
obj_dict = dict([(obj.id, obj) for obj in qs])
objects = MyObject.objects.filter(myrelatedobj__in=qs)
relation_dict = {}
for obj in objects:
    relation_dict.setdefault(obj.myobject_id, []).append(obj)
for id, related_items in relation_dict.items():
    obj_dict[id].related_items = related_items

I wrote a blogpost about this a while ago, you can find more info here: http://bit.ly/ge59D2

以为你会在 2024-10-26 10:42:01

怎么样:

使用 Django 的 会话框架;启动时加载列表 request.session['lstTechnology'] = listOfTechnology。并在应用程序的其余部分使用会话。

How about:

Using Django's session framework; load list request.session['lstTechnology'] = listOfTechnology on startup. And use session in rest of the app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文