I have the following model with a m2m field where logged in Users can show interest in a publication:
models.py
from django.db import models
class Publication:
title = models.CharField(max_lenth=512)
users_interested = models.ManyToManyField(User)
views.py
from django.shortcuts import render
from django.views import View
from .models import Publication
class listPublicationView(View):
def get(self, request, *args, **kwargs):
publications = Publication.objects.all()
return render(request, "base.html", {'publications': publications})
Now i try to produce a "i am already interested" in the template when a logged in user is already interested in the publication:
base.html
{% for publication in publications %}
{{publication.title}}
{% if currently logged in User is interested in publication (check users_interested) %}
i am already interested
{% endif %}
{% endfor %}
I think about something like this:
{% if user.id in publication.users_interested__id %}
This seems to look like a good solution:
models.py
view.py
base.html
Found this solution in Django: check for value in ManyToMany field in template