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?