Template tag to check if object in queryset

285 views
Skip to first unread message

dadapapa

unread,
Nov 17, 2009, 11:07:04 AM11/17/09
to Django users
Dear list,

I have two models that are in the following relationship:

class Member(django.contrib.auth.models.User) :
# some app specific stuff here
pass

class Event(django.db.models.Model) :
attendees = models.ManyToManyRelation(Member)
# some other fields


In my template, I want to iterate over an Event QuerySet and render
the
output differently depending on whether the currently logged in member
attends an event or not:

{% for event in event_list %}
{% if request.user.member in event.attendees %}
render something here
{% else %}
render something else
{% endif %}
{% endfor %}

Obviously, the if tag that tests for attendance is ill-formatted, but
I cannot
figure out the correct solution. I went through the documentation and
googled
for a solution but cannot find it. Do I really need to write a custom
{% ifcontains event.attendees request.user.member %} tag that would
check if
a given queryset contains an object, or am I missing something the
obvious?

Thanks.

Tom Evans

unread,
Nov 17, 2009, 11:15:34 AM11/17/09
to django...@googlegroups.com
Yes, unfortunately you do. See [1] and also the 'smart if' tag on djangosnippets [2], which is potentially being added to django 1.2.

Cheers

Tom

[1] http://code.djangoproject.com/ticket/8087
[2] http://www.djangosnippets.org/snippets/1350/

dadapapa

unread,
Nov 17, 2009, 7:01:00 PM11/17/09
to Django users
Thanks for the pointers. This is exactly what I want and have to do
then ... fingers crossed for inclusion into django1.2!

On Nov 17, 5:15 pm, Tom Evans <tevans...@googlemail.com> wrote:
Reply all
Reply to author
Forward
0 new messages