开发者

Django - ForeignKey issue. How many DB accesses?

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, ther开发者_Python百科e 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?


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/


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.


articles = Article.objects.all(paper__city__name='Toronto', paper__price__lt=5)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜