Reputation: 1360
So I have a ManyToManyField relationship between Item1 and Item2. On a webpage, I want to display one of two messages based on whether the two items are connected or not. I'm just not sure how to query my exact item using the {% if %} template tag.
Roughly what I'm looking for is
{% if Item1 is connected to Item2 %} Display Message1
{% else %} Display Message2 {% endif %}
Any tips on how I'd get this done?
class Profile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=50)
eventList = models.ManyToManyField(Event, blank="TRUE", null="TRUE", related_name='event_set+')
def __unicode__(self):
return self.name
Upvotes: 5
Views: 1251
Reputation: 29804
It still not clear to me what object you want to see if connected to other but if you want to know if a user is in an specific event you can do it like this:
{% if event in user.eventList.all %}
Display Message1
{% else %}
Display Message2
{% endif %}
You can use operator in
in if
conditions in modern django versions.
Hope this helps!
Upvotes: 6