Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 检查用户是否在多个字段中_Django_M2m - Fatal编程技术网

Django 检查用户是否在多个字段中

Django 检查用户是否在多个字段中,django,m2m,Django,M2m,我有以下带有m2m字段的模型,登录用户可以在其中显示对出版物的兴趣: 型号.py from django.db import models class Publication: title = models.CharField(max_lenth=512) users_interested = models.ManyToManyField(User) from django.shortcuts import render from django.views import View f

我有以下带有m2m字段的模型,登录用户可以在其中显示对出版物的兴趣:

型号.py

from django.db import models

class Publication:
  title = models.CharField(max_lenth=512)
  users_interested = models.ManyToManyField(User)
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})
from django.db import models

class Publication:
  title = models.CharField(max_lenth=512)

  #added a reverse accessor
  users_interested = models.ManyToManyField(User, related_name='users_interested')
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()

    # create a set of group IDs that this user is a part of
    current_user = request.user
    user_publication_set = set(current_user.users_interested.values_list('id', flat=True))

    #pass set to template
    return render(request, "base.html", {'publications': publications, 'user_publication_set': user_publication_set})
视图.py

from django.db import models

class Publication:
  title = models.CharField(max_lenth=512)
  users_interested = models.ManyToManyField(User)
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})
from django.db import models

class Publication:
  title = models.CharField(max_lenth=512)

  #added a reverse accessor
  users_interested = models.ManyToManyField(User, related_name='users_interested')
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()

    # create a set of group IDs that this user is a part of
    current_user = request.user
    user_publication_set = set(current_user.users_interested.values_list('id', flat=True))

    #pass set to template
    return render(request, "base.html", {'publications': publications, 'user_publication_set': user_publication_set})
现在,当登录用户已经对出版物感兴趣时,我尝试在模板中生成“我已经感兴趣”:

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 %}
{% for publication in publications %}

  {{publication.title}}

  {% if publication.id in user_publication_set %}
      i am already interested
  {% endif %}

{% endfor %}
我会这样想:

{% if user.id in publication.users_interested__id %}
{% if request.user in publication.users_interested.all %}
试着这样做:

{% if user.id in publication.users_interested__id %}
{% if request.user in publication.users_interested.all %}
  • request.user
    属性保存当前登录的用户
  • 然后将
    中的
    运算符用于
    出版物.用户感兴趣的.all()
    (请注意,模板中的
    .all()
    上没有括号
试着这样做:

{% if user.id in publication.users_interested__id %}
{% if request.user in publication.users_interested.all %}
  • request.user
    属性保存当前登录的用户
  • 然后将
    中的
    运算符用于
    出版物.用户感兴趣的.all()
    (请注意,模板中的
    .all()
    上没有括号

    • 这似乎是一个很好的解决方案:

      型号.py

      from django.db import models
      
      class Publication:
        title = models.CharField(max_lenth=512)
        users_interested = models.ManyToManyField(User)
      
      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})
      
      from django.db import models
      
      class Publication:
        title = models.CharField(max_lenth=512)
      
        #added a reverse accessor
        users_interested = models.ManyToManyField(User, related_name='users_interested')
      
      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()
      
          # create a set of group IDs that this user is a part of
          current_user = request.user
          user_publication_set = set(current_user.users_interested.values_list('id', flat=True))
      
          #pass set to template
          return render(request, "base.html", {'publications': publications, 'user_publication_set': user_publication_set})
      
      view.py

      from django.db import models
      
      class Publication:
        title = models.CharField(max_lenth=512)
        users_interested = models.ManyToManyField(User)
      
      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})
      
      from django.db import models
      
      class Publication:
        title = models.CharField(max_lenth=512)
      
        #added a reverse accessor
        users_interested = models.ManyToManyField(User, related_name='users_interested')
      
      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()
      
          # create a set of group IDs that this user is a part of
          current_user = request.user
          user_publication_set = set(current_user.users_interested.values_list('id', flat=True))
      
          #pass set to template
          return render(request, "base.html", {'publications': publications, 'user_publication_set': user_publication_set})
      
      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 %}
      
      {% for publication in publications %}
      
        {{publication.title}}
      
        {% if publication.id in user_publication_set %}
            i am already interested
        {% endif %}
      
      {% endfor %}
      

      中找到此解决方案这似乎是一个不错的解决方案:

      型号.py

      from django.db import models
      
      class Publication:
        title = models.CharField(max_lenth=512)
        users_interested = models.ManyToManyField(User)
      
      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})
      
      from django.db import models
      
      class Publication:
        title = models.CharField(max_lenth=512)
      
        #added a reverse accessor
        users_interested = models.ManyToManyField(User, related_name='users_interested')
      
      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()
      
          # create a set of group IDs that this user is a part of
          current_user = request.user
          user_publication_set = set(current_user.users_interested.values_list('id', flat=True))
      
          #pass set to template
          return render(request, "base.html", {'publications': publications, 'user_publication_set': user_publication_set})
      
      view.py

      from django.db import models
      
      class Publication:
        title = models.CharField(max_lenth=512)
        users_interested = models.ManyToManyField(User)
      
      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})
      
      from django.db import models
      
      class Publication:
        title = models.CharField(max_lenth=512)
      
        #added a reverse accessor
        users_interested = models.ManyToManyField(User, related_name='users_interested')
      
      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()
      
          # create a set of group IDs that this user is a part of
          current_user = request.user
          user_publication_set = set(current_user.users_interested.values_list('id', flat=True))
      
          #pass set to template
          return render(request, "base.html", {'publications': publications, 'user_publication_set': user_publication_set})
      
      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 %}
      
      {% for publication in publications %}
      
        {{publication.title}}
      
        {% if publication.id in user_publication_set %}
            i am already interested
        {% endif %}
      
      {% endfor %}
      

      中找到此解决方案,您必须在视图中加载所需的信息。模板不是用来生成任何内容的(除了html、ofc)。在您的情况下,可能是某种形式的
      prefetch\u related()
      annotate()
      。您必须在视图中加载所需的信息。模板不是用来生成任何内容的(但是html,ofc)。在您的情况下,可能是某种形式的
      预取相关()
      注释()
      。在他的模板中有这个循环,如果他有,比如说,1000份出版物,他的页面会突然用1001个查询压碎数据库。这是可行的——但我真的会有几百份出版物,在他的模板中有这个循环,如果他有,比如说,1000份出版物,他的页面会突然用1001个查询压碎数据库ks--但我会有几百份出版物,这确实是一个更好的解决方案。重点是始终让视图确定应该加载哪些数据,这样它就可以控制逻辑。这正是您所做的。这确实是一个更好的解决方案。重点是始终让视图确定应该加载哪些数据,这样它就可以控制逻辑控制逻辑,你就是这么做的。