如何在manytomany字段的查询中只有一个结果?

时间:2021-10-13 19:37:13

user 1 have option A and C, user 2 have option A and B

用户1有选项A和C,用户2有选项A和B.

if user A is logued, he can see this

如果用户A被注册,他可以看到这一点

  • option A - you have
  • 选项A - 你有

  • option B - you like this? (with a button for add)
  • 选项B - 你喜欢这个吗? (带按钮添加)

  • option C - you have
  • 选项C - 你有

  • option D - you like this? (with a button for add)
  • 选项D - 你喜欢这个吗? (带按钮添加)

  • option E - you like this? (with a button for add)
  • 选项E - 你喜欢这个吗? (带按钮添加)

if user 2 is logued, he can see this

如果用户2被注册,他可以看到这一点

  • option A - you have
  • 选项A - 你有

  • option B - you have
  • 选项B - 你有

  • option C - you like this? (with a button for add)
  • 选项C - 你喜欢这个吗? (带按钮添加)

  • option D - you like this? (with a button for add)
  • 选项D - 你喜欢这个吗? (带按钮添加)

  • option E - you like this? (with a button for add)
  • 选项E - 你喜欢这个吗? (带按钮添加)

but only can do this:

但只能做到这一点:

if user 1 is logued:

如果用户1已注销:

  • option A - you have - you like this? (with a button for add)
  • 选项A - 你有 - 你喜欢这个吗? (带按钮添加)

  • option B - you like this? (with a button for add) - you like this? (with a button for add)
  • 选项B - 你喜欢这个吗? (带按钮添加) - 你喜欢这个吗? (带按钮添加)

  • option C - you have - you like this? (with a button for add)
  • 选项C - 你有 - 你喜欢这个吗? (带按钮添加)

  • option D - you like this? (with a button for add) - you like this? (with a button for add)
  • 选项D - 你喜欢这个吗? (带按钮添加) - 你喜欢这个吗? (带按钮添加)

  • option E - you like this? (with a button for add) - you like this? (with a button for add)
  • 选项E - 你喜欢这个吗? (带按钮添加) - 你喜欢这个吗? (带按钮添加)

and user 2 :

和用户2:

  • option A - you have - you like this? (with a button for add)
  • 选项A - 你有 - 你喜欢这个吗? (带按钮添加)

  • option B - you have - you like this? (with a button for add)
  • 选项B - 你有 - 你喜欢这个吗? (带按钮添加)

  • option C - you like this? (with a button for add) - you like this? (with a button for add)
  • 选项C - 你喜欢这个吗? (带按钮添加) - 你喜欢这个吗? (带按钮添加)

  • option D - you like this? (with a button for add) - you like this? (with a button for add)
  • 选项D - 你喜欢这个吗? (带按钮添加) - 你喜欢这个吗? (带按钮添加)

  • option E - you like this? (with a button for add)
  • 选项E - 你喜欢这个吗? (带按钮添加)

my models:

class Options(models.Model):
    option_titulo = models.Charfield(max_lenght=100)
    user = models.ManyToManyField(User, null=True, blank=True, default=None)

my view:

def ListaOptions(request):
    userid=request.user.id
    options=Options.objects.all()
    opt_user=Options.objects.filter(user__id=userid)

    return render_to_response(
      'options.html',
       {
        'options': options,
        'opt_user': opt_user,
       },
       context_instance=RequestContext(request)
                )

my template:

 {% for o in options %}

   {{ o.option_titulo }} - 

   {% for u in opt_user %}

     {% if u.id == o.id %}
      you have
     {% else %}
      you like this? (with a button for add)
     {% endif %}

   {% endfor %}

 {% endfor %}

1 个解决方案

#1


0  

You could use in for each option to test if it's in opt_user:

您可以使用in为每个选项测试它是否在opt_user中:

{% for o in options %}
    {{ o.option_titulo }} - 
    {% if o in opt_user %}
        you have
    {% else %}
        you like this? (with a button for add)
    {% endif %}
{% endfor %}

#1


0  

You could use in for each option to test if it's in opt_user:

您可以使用in为每个选项测试它是否在opt_user中:

{% for o in options %}
    {{ o.option_titulo }} - 
    {% if o in opt_user %}
        you have
    {% else %}
        you like this? (with a button for add)
    {% endif %}
{% endfor %}