On Mon, 19 Jul 2010 00:50:04 +0200, Martin Tiršel <[email protected]>
wrote:
Hello,
I have:
class Gallery(models.Model):
...
class Image(models.Model):
gallery = models.ForeignKey(Gallery)
...
Now, on some page, I am listing all galleries and I want for every
gallery first three (or three flagged, ...) images as thumbnails. I can
go through each gallery and make another query to load these three
images, but it isn't very effective. I come from PHP, there I would do
such things:
SELECT * FROM `Gallery`
Then create an instance of Gallery object, take all galleries IDs and
make second query:
SELECT * FROM `Image` WHERE `id_gallery` IN (...)
where ... are all Galleries IDs from previous query. Then create
instances of Image object and connect it with appropriate Gallery object.
1. How to do such thing in Django?
2. And second question, does Gallery knows about Image? In other words,
if I have an Gallery object (e.g. "gal"), can I access to Image through
Gallery? Something like gal.Image[0] for first image in the gallery.
Thanks,
Martin
I will answer myself :)
2. gallery.image_set is the answer
1. gallery.image_set.add(), so I load Galleries first, then Images with
another query, iterate through all images and every image insert into
appropriate gallery object. If there is a better way to do this, please
let me know.
Martin
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.