Django - 外键问题。有多少数据库访问?

发布于 2024-11-19 18:08:53 字数 1111 浏览 3 评论 0原文

我目前正在使用 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 技术交流群。

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

发布评论

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

评论(3

初熏 2024-11-26 18:08:54

我不能使用papers.article_set,因为papers是一个QuerySet。如果我
尝试使用循环,它可能会每次查询一次
纸质物体,对吗?

如果循环查询集,则只会执行一条 SQL 语句。
Django 缓存整个查询集,但如果只有 1000 行,这不会有问题。

如果循环大型查询集,请使用 queryset.iterator():

https://docs .djangoproject.com/en/1.3/topics/db/optimization/

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?

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/

妖妓 2024-11-26 18:08:54

您可以像这样获取执行的查询(确保在 settings.py 中 DEBUG=True ):

 from django.db import connection
 connection.queries

更多详细信息可以在 Django DB 常见问题解答

You can get the executed queries like this (make sure DEBUG=True in your settings.py):

 from django.db import connection
 connection.queries

More details can be found in the Django DB FAQ.

梦境 2024-11-26 18:08:54
articles = Article.objects.all(paper__city__name='Toronto', paper__price__lt=5)
articles = Article.objects.all(paper__city__name='Toronto', paper__price__lt=5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文