Checking for an item in multi-user mode (django)

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 elements are connected or not. I just don’t know how to request my exact element using the template tag {% if%}.

Roughly what I'm looking for,

{% if Item1 is connected to Item2 %} Display Message1 {% else %} Display Message2 {% endif %} 

Any tips on how I will do this?

 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 
+4
source share
1 answer

It’s still not clear to me which object you want to see if it is connected to another, but if you want to find out if the user is in a particular event, you can do it as follows:

 {% if event in user.eventList.all %} Display Message1 {% else %} Display Message2 {% endif %} 

You can use the in operator in if conditions in modern versions of django.

Hope this helps!

+3
source

Source: https://habr.com/ru/post/1486293/


All Articles