Django - 外键问题。有多少数据库访问?
我目前正在使用 Django,我的模型是这样的。
class City(models.Model):
name = models.CharField(max_length=255, primary_key=True)
url = models.URLField()
class Paper(models.Model):
city = models.ForeignKey(City)
name = models.CharField(max_length=255)
price = models.IntegerField()
class Article(models.Model):
paper = models.ForeignKey(Paper)
name = models.CharField(max_length=255)
我试图通过过滤城市名称和纸张价格来获取一个城市对象、几个纸张对象和几个文章对象。
要搜索 City 表,我可以这样做:
cities = City.objects.get(pk='Toronto')
要获取 Paper 对象:
papers = Paper.objects.filter(city=cities, price < 5)
或者我什至可以将两者结合起来:(
papers = cities.paper_set.filter(city=cities, price < 5)
这会更有效吗?)
问题是找到一种有效的方法来获取上面的所有文章 '文件'。
我不能使用 paper.article_set 因为 paper 是一个 QuerySet。如果我尝试使用循环,它可能会对每个纸质对象进行一次查询,对吗?
仅供参考,City 表有 1000 列,每个 City 有 1-1000 个 Paper 对象,每个 Paper 对象大约有 10 个 Article 对象。
任何帮助将不胜感激。
谢谢。
编辑: 假设我有一个城市查询集(上面),有没有办法在单个查询中获取所有 Article 对象?
I am currently using Django and my model is like this.
class City(models.Model):
name = models.CharField(max_length=255, primary_key=True)
url = models.URLField()
class Paper(models.Model):
city = models.ForeignKey(City)
name = models.CharField(max_length=255)
price = models.IntegerField()
class Article(models.Model):
paper = models.ForeignKey(Paper)
name = models.CharField(max_length=255)
I am trying to get a City object, several Paper objects and several Article objects by filtering through the City name and the price of the Paper.
To search through the City table I can do this:
cities = City.objects.get(pk='Toronto')
To get the Paper objects:
papers = Paper.objects.filter(city=cities, price < 5)
or I could even combine the two:
papers = cities.paper_set.filter(city=cities, price < 5)
(Will this be more efficient?)
The problem is to find an efficient way to get all the articles from the above 'papers'.
I can't use papers.article_set since papers is a QuerySet. And if I try to use a loop it would probably be making the queries once per paper object, right?
Just for reference the City table has 1000 columns, there are 1-1000 Paper objects per City, and around 10 Article objects per Paper object.
Any help will be really appreciated.
Thank you.
Edit:
Assuming I have a cities QuerySet (above), is there a way to get all the Article objects in a single query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果循环查询集,则只会执行一条 SQL 语句。
Django 缓存整个查询集,但如果只有 1000 行,这不会有问题。
如果循环大型查询集,请使用 queryset.iterator():
https://docs .djangoproject.com/en/1.3/topics/db/optimization/
If you loop over a queryset only one SQL statement gets executed.
Django caches the whole queryset, but if you have only 1000 rows, this will be no problem.
If you loop over large querysets use queryset.iterator():
https://docs.djangoproject.com/en/1.3/topics/db/optimization/
您可以像这样获取执行的查询(确保在 settings.py 中 DEBUG=True ):
更多详细信息可以在 Django DB 常见问题解答。
You can get the executed queries like this (make sure DEBUG=True in your settings.py):
More details can be found in the Django DB FAQ.