Есть quetryset из пользователей и циклом отображен в html. Как узнать что залогиненный пользователь подписан на пользователя из списка?
views.py
def is_valid_queryset_param_stroke(param):
return param != '' and param is not None
class SearchUsers(ListView):
model = CustomUser
template_name = 'search/search.html'
context_object_name = 'users'
def get_queryset(self):
qs = CustomUser.objects.all()
search_input = self.request.GET.get('search-input')
if is_valid_queryset_param_stroke(search_input):
qs = qs.annotate(similarity=TrigramWordSimilarity(search_input, 'username'),).filter(similarity__gt=0.3).order_by('-similarity')
return qs
models.py | юзера
class CustomUser(AbstractUser):
"""Extended user model"""
slug = models.SlugField(db_index=True, unique=True, null=True)
avatar = models.ImageField(upload_to='user_avatar/', verbose_name='User image', null=True, blank=True,
default='user_avatar/blank-profile-picture.png')
about_me = models.TextField(verbose_name='About User', null=True, blank=True)
city = models.CharField(max_length=255, verbose_name='City', null=True)
country = models.CharField(max_length=255, verbose_name='Country', null=True)
phone = models.CharField(max_length=25, blank=True, null=True, verbose_name='Phone number')
class Meta:
db_table = 'CustomUser'
def save(self, *args, **kwargs):
self.slug = self.username
return super(CustomUser, self).save(*args, **kwargs)
models.py | контактов
class Contact(models.Model):
"""User contact model"""
user_from = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='contact_from')
user_to = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='contact_to')
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-created', )
db_table = 'Contact'
html:
{% for u in users %}
<div class="p-2 mt-3 border d-flex">
<a href="{% url 'profile' slug=u.slug %}" class="text-decoration-none text-dark">
<img src="{% static u.avatar.url %}" width="100px" height="100px">
</a>
<div class="user-info ms-5">
<h5>{{ u.first_name }} {{ u.last_name }}</h5>
<h6>{{ u.country }}, {{ u.city }}</h6>
<p>Подписчиков: {{ u.contact_to.all.count }}</p>
</div>
<div class="btns">
{% if not request.user == u %}
<button id="add-contact-btn"
class="btn btn-dark"
data-action="{% if request.user in u.contact_to.all.id %}un{% endif %}follow"
data-id="{{ user.id }}">
{% if request.user in u.contact_to.all %}Отписаться{% else %}Подписаться{% endif %}
</button>
<a href="{% url 'chat_create' user_id=u.id %}" class="btn btn-outline-dark">Сообщение</a>
{% endif %}
</div>
</div>
</a>
{% endfor %}