Hello

I have two models and 1-to-many relation between them, using ForeignKey (gallery with pictures). Additionally instances on 'many' side are ordered_with_respect_to owning instance. I need to iterate over them in template, respecting imposed order. I can use "get_pictureingallery_order" on owner instance but all I get is IDs, not instance itself. Moreover, situation like this repeats itself, each of instances on 'many' side has (ordered) many instances of a third model, also ordered (subtitles for a piture). And these also need to be iterated over with respect to imposed order.

How to do it?

Simplified code:

# models.py

class Gallery(models.Model):
    ...

class PictureInGallery(models.Model):

    picture = models.ForeignKey(Picture)
    gallery = models.ForeignKey(Gallery)

    class Meta:
        order_with_respect_to = 'gallery'
    ...

class PictureSubtitle(models.Model):

    picture_in_gallery = models.ForeignKey(PictureInGallery)
    subtitle = models.CharField(max_length=50)

    class Meta:
        order_with_respect_to = 'picture_in_gallery'

# gallery.html
# iteration without respect to order (yields improper order)

{% for picture_in_gallery in gallery.pictureingallery_set.all %}

<img src="{{picture_in_gallery.picture.image.url}}" />

   {% for subtitle in picture_in_gallery.picturesubtitle_set.all %}
       {{subtitle}}
   {% endfor %}

{% endfor %}

# iteration with respect to order (yields proper order of IDs but doesn't yield instances)

{% for picture_in_gallery_id in gallery.get_pictureingallery_order %}
     {{picture_in_gallery_id}}
{% endfor %}


Thanks in advance

Marek

--
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