foreign keys - Django - ForeignKey issue. How many DB accesses? -
foreign keys - Django - ForeignKey issue. How many DB accesses? -
i using django , model 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) cost = models.integerfield() class article(models.model): paper = models.foreignkey(paper) name = models.charfield(max_length=255) i trying city object, several paper objects , several article objects filtering through city name , cost of paper.
to search through city table can this:
cities = city.objects.get(pk='toronto') to paper objects:
papers = paper.objects.filter(city=cities, cost < 5) or combine two:
papers = cities.paper_set.filter(city=cities, cost < 5) (will more efficient?)
the problem find efficient way articles above 'papers'.
i can't utilize papers.article_set since papers queryset. , if seek utilize loop making queries 1 time per paper object, right?
just reference city table has 1000 columns, there 1-1000 paper objects per city, , around 10 article objects per paper object.
any help appreciated.
thank you.
edit: assuming have cities queryset (above), there way article objects in single query?
articles = article.objects.all(paper__city__name='toronto', paper__price__lt=5)
django foreign-keys django-database
Comments
Post a Comment