On Oct 19, 9:46 pm, Ed <edmund.rog...@gmail.com> wrote:
> Thanks for the answers.  I thought that the view was already pulling
> the data.  I didn't realize it gets pulled when called in the
> template.  I was able to rewrite the view to provide the data more
> efficiently.
>
> I have another related question on the topic of query expense.  What
> is the best practice as far as database table design?
>
> I have 3 tables: studio, film, images.  If film has a foreign key to
> studio, and images has a foreign key to film.  If I wanted to pull all
> of the images for a particular studio, it would be more expensive to
> pull:
>
> all films (given studio), and then films.images_set
>
> as opposed to:
>
> including the studio in the image table and then pulling all images
> (given studio).
>
> But there is some data replication here.  Technically, I can discover
> studio by following the link from image to film to studio.  But it I
> need less queries given this particular need.  What is the best
> practice in a scenario like this?

Well, as you've realised, iterating through each film to get its
images will fire off one query per film. But how to do it efficiently
depends on what you're doing with the data. If you just need all
images for one studio, and don't care about the films, then one query
will suffice:

    Image.objects.filter(film__studio=mystudio)

If you need all studios and their related images, it's a bit more
difficult. One way of doing it would be to use the queryset `extra`
clause to add a studio ID to each image, group them by that ID, then
studios appending the image list to each one. I can post some code if
that's the sort of thing you need to do.
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to