Django反向查询链接查询 [英] Django reverse lookup chaining queryset

查看:141
本文介绍了Django反向查询链接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个模型,我需要做反向查找,然后以某种方式链接在一起的查询。



我有3个模型,(1)设计师创建(2)项目,项目包含(3)上传的图像。



模型

  
...

class Project(models.Model):
designer = models.ForeignKey('Designer')

已上传的图像(models.Model):
project = models.ForeignKey('Project')

所以一个设计师打开他的页面,并希望看到他的所有项目和一些与他的项目相关联的图像,我可以做一些像

  d = Designer.objects.get(id = 2)
projects = d.project_set.all()

但是,随着图像,我必须做这个事情很多次,每次打数据库。

  images = [] 
for p在项目中:
images.append(p.uploadedimage_set.all ())

现在我们有另一个问题,我该如何连接图片项目 ??我可以通过建立一个这样的字典做一些愚蠢的事情,

  images = [] 
for p in project:
images.append({p.id:p.uploadedimage_set.all()})

然后,当我重复执行项目时,我可以使用 id 找出哪些图像与哪个项目相关联。



没有什么比较优雅的东西让我去,


  1. 点击数据库一次

  2. 允许我做一些看起来像 d.projects [0] .images [0] ???而不是建立某种愚蠢的自定义字典? Django是否有某种查询建立工具?

谢谢!

解决方案

如果您只是在与特定设计器相关的图像之后,您可以使用单个查询来获取它们: p>

  designer = Designer.objects.get(id = id)
images = UploadedImage.objects.filter(product__designer = design)

如果你想将它们与项目相关联,有几种方法可以做到这一点。 / p>

如果没有很多不同的项目,并且担心数据库的查询太多,可以使用图像列表,并在python中执行过滤:

  images = list(images)#结晶查询
projectImages = [[img for img in images if img.project == p]
for p in designer.project_set.all()]

然而,一个更好的方法可能是让数据库处理过滤;对于一组非常小的尺寸,它可能会更快。



这样,你可以直接查询图像,即:

  designer = Designer.objects.get(id = id)
projects = designer.project_set.all()#Project.objects.filter(designer =设计师)
projectImages =项目中p的[UploadedImage.objects.filter(project = p)]

第二种方法的最大优点是 projectImages 中的元素仍然是查询集,因此仍可以进一步过滤或注释。


I have several models that I need to do reverse lookups in, then somehow chain the querysets together.

I got 3 models, (1) designers create (2) projects, and projects contain (3) uploaded images.

Models

class Designer(models.Model):
    ... 

class Project(models.Model):
    designer = models.ForeignKey('Designer')

class UploadedImage(models.Model):
    project = models.ForeignKey('Project')

So a designer opens up his page and wants to see all his projects and some images associated with his projects, I could do something like,

d = Designer.objects.get(id=2)
projects = d.project_set.all()

But then with images, I gotta do this thing many many times, each time hitting the database.

images = []
for p in projects:
    images.append(p.uploadedimage_set.all())

Now we have another problem, which is how do I connect images and projects?? I could do something stupid by building up a dictionary like this,

images = []
for p in projects:
    images.append( { p.id : p.uploadedimage_set.all() } )

Then when I iterate through projects, I could just use the id to figure out which images are associated with which project.

Isn't there something a lot more elegant that'll allow me to,

  1. Hit the database just once
  2. Allow me to do something that'll look like d.projects[0].images[0]??? Instead of building up some sort of a stupid custom dictionary??? Does Django have some sort of queryset building tool or whatever?

Thanks!!

解决方案

If you're just after the images associated with a particular designer, you can grab them with a single query:

designer = Designer.objects.get(id=id)
images = UploadedImage.objects.filter(product__designer=designer)

If you would like to associate them with the project, there's a couple of ways to do that.

If there aren't many different projects, and you're concerned about querying the database too much, you can use the images list from before, and perform the filtering in python:

images = list(images) # Crystallise the query
projectImages = [[img for img in images if img.project == p]
                 for p in designer.project_set.all()]

However, a better way to do it would probably be to let the database handle the filtering; for sets of a non-trivial size it will likely be faster.

That way, you can just query for the images directly, ie:

designer = Designer.objects.get(id=id)
projects = designer.project_set.all() # Project.objects.filter(designer=designer)
projectImages = [UploadedImage.objects.filter(project=p) for p in projects]

The big advantage of the second approach is that the elements in projectImages are still query sets, so can still be further filtered or annotated.

这篇关于Django反向查询链接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆