Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Filtering related objects in template [RSS Feed]

#1 Jan. 8, 2011 18:56:45

B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Filtering related objects in template


Can I filtering of related objects in a template? Explained:
Now I make so

{% for image in product.image_set.all %}
{% if image.image_type.id == 777 %}
<img...................>
{% endif %}
{% endfor %}

And I would like to:

{% for image in product.image_set.all | image_type.id : 777 %}
<img...................>
{% endfor %}

----------------------------------------------------------------------------------
(sorry for my English %))

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 8, 2011 20:06:03

+.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Filtering related objects in template


>From Stackoverflow - How do I perform query filtering in django
>templates<http://stackoverflow.com/questions/223990/how-do-i-perform-query-filtering-in-django-templates>
:

You can't do this, which is by design. The Django framework authors intended
a strict separation of presentation code from data logic. Filtering models
is data logic, and outputting HTML is presentation logic.

So you have several options. The easiest is to do the filtering, then pass
the result to render_to_response. Or you could write a method in your model
so that you can say {% for object in data.filtered_set %}. Finally, you
could write your own template tag, although in this specific case I would
advise against that.

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Filtering related objects in template [RSS Feed]

Board footer

Moderator control

Enjoy the 19th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support