Rocking Me
Rocking Me

Reputation: 162

Custom template tags

I have a model name "Attendee". In that there are foreignkeys as user and model named "Event".

Now I have to get the users of Attendee who are going for event.

{% get_event_attendee event as attending_event %}

it works for me. But I need this step with "for",
I mean

{% get_event_attendee for event as attending_event %}

Please help.

Thanks

Upvotes: 0

Views: 50

Answers (2)

user4423327
user4423327

Reputation:

Here you go.

https://djangosnippets.org/snippets/1919/

Previous I had a damn thing to do. Go through with that you will definitely crack that :)

Upvotes: 1

Norman8054
Norman8054

Reputation: 824

If your models look like this

from django.db import models

class User(models.Model):
    ...


class Event(models.Model):
    ...


class Attendee(models.Model):
    user = models.ForeignKey(User)
    event = models.ForeignKey(Event)

you can access the attendees in the template like this:

{% for attendee in event.attendee_set.all %}
    {{ attendee.user }}
{% endfor %}

Is this your question?

If you have multiple events you can use two forloops:

{% for event in event_list %}
    {% for attendee in event.attendee_set.all %}
        {{ attendee.user }}
    {% endfor %}
{% endfor %}

Upvotes: 1

Related Questions