简体   繁体   English

如何在搜索用户中过滤掉用户的朋友 function Django

[英]How to filter out Friends of user in search users function Django

I'm trying to filter out the friends of a user and also the current logged in user from a "search_users" function, I've tried using exclude() but keep getting an error I'm not sure whats wrong.我正在尝试从“search_users”function 中过滤掉用户的朋友以及当前登录的用户,我尝试使用 exclude(),但不断收到错误,我不确定出了什么问题。 I also wanted to add a "add friend" button next to the users, which I think I've done correctly on 'search_users.html.我还想在用户旁边添加一个“添加朋友”按钮,我认为我在“search_users.html”上做得正确。 Error错误

views.py视图.py

@login_required
def search_users(request):
    query = request.GET.get('q')
    object_list = User.objects.filter(username__icontains=query).exclude(friends=request.user.profile.friends.all())
    context ={ 
        'users': object_list
    }
    return render(request, "users/search_users.html", context)

search_users.html search_users.html

{% extends "feed/layout.html" %} {% load static %}
{% block searchform %}
<form
  class="form-inline my-2 my-lg-0 ml-5"
  action="{% url 'search_users' %}"
  method="get"
>
  <input name="q" type="text" placeholder="Search users.." />
  <button class="btn btn-success my-2 my-sm-0 ml-10" type="submit">
    Search
  </button>
</form>
{% endblock searchform %} {% block content %}
<div class="container">
  <div class="row">
    <div class="col-md-8">
      {% if not users %}
      <br /><br />
      <h2><i>No such users found!</i></h2>
      {% else %}
      <div class="card card-signin my-5">
        <div class="card-body">
          {% for user_p in users %}
          
          <a href="{{ user_p.profile.get_absolute_url }}"
            ><img
              src="{{ user_p.profile.image.url }}"
              class="rounded mr-2"
              width="40"
              height="40"
              alt=""
          /></a>  
          <a class="text-dark" href="{{ user_p.profile.get_absolute_url }}"
            ><b>{{ user_p }}</b></a
            >  
            
            <small class="float-right">
              <a
                class="btn btn-primary mr-2"
                href="/users/friend-request/send/{{ user_p.username }}"
                >Add Friend</a>
              </small>
          
          <br/><br />

          {% endfor %}
        </div>
      </div>
      {% endif %}
    </div>

    <div class="col-md-4">
      <div class="card card-signin my-5">
        <a href="{{ request.user.profile.get_absolute_url }}"
          ><img
            class="card-img-top"
            src="{{ request.user.profile.image.url }}"
            alt=""
        /></a>
        <div class="card-body">
          <h5 class="card-title text-center">{{ request.user }}</h5>
          <h6 class="text-center">
            {{ request.user.profile.friends.count }}
            <p class="text-muted">Friends</p>
          </h6>
          <p class="card-text text-center">{{ request.user.profile.bio }}</p>
        </div>
      </div>
    </div>
  </div>
  {% endblock content %}
</div>

models.py模型.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.png', upload_to='profile_pics')
    slug = AutoSlugField(populate_from='user')
    bio = models.CharField(max_length=255, blank=True)
    friends = models.ManyToManyField('Profile', blank=True)

It should be它应该是

User.profile.objects.filter(username__icontains=query).exclude(friends=request.user.profile.friends.all())

You were getting an error earlier because you referred to the user object earlier and not the profile itself.您之前遇到了错误,因为您之前引用了用户 object 而不是配置文件本身。 User.profile gives the one to one related model profile instead. User.profile提供一对一相关的 model 配置文件。

You can read more about one to one relationships here.您可以在此处阅读有关一对一关系的更多信息。 https://docs.djangoproject.com/en/3.1/topics/db/examples/one_to_one/ https://docs.djangoproject.com/en/3.1/topics/db/examples/one_to_one/

I would have done like this:我会这样做:

object_list = User.objects\
    .filter(username__icontains=query)\
    .exclude(profile__friends__in=request.user.profile.friends.all())\
    .exclude(id=request.user.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django查询以从用户列表中过滤出当前用户 - Django query to filter out current user from a list of users Django:按用户角色过滤用户 - Django: Filter users by user role Django:覆盖用户查询集以从公众中过滤掉管理员/员工用户? - Django: Override user queryset to filter out admin/staff users from the public? 我无法在 django 中进行查询以过滤掉当前登录用户正在关注的那些用户的帖子 - I can't make query in django to filter out posts from those users that the currently logged in user is following Django Howto筛选出属于特定国家/地区的用户,而不是所有用户 - Django Howto Filter out the users that belongs to a particular country and not all users 在Python / Django中将用户的facebook / twitter朋友与站点用户进行比较 - Comparing user's facebook/twitter friends to site's users in Python/Django 在Django中找到用户的Facebook朋友,他们也是我网站上的注册用户 - Find user's Facebook friends who are also registered users on my site in Django Django ORM查询用户的朋友 - Django ORM query for friends of a user Django-如何检索已登录用户好友的帖子数据 - Django-How to retrieve posts data of logged in user friends Django-Simple-Friends所有未作为朋友连接的用户 - Django-Simple-Friends all users not connected as friends
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM