如果x在< listOfModels.field>句法 [英] If x in <listOfModels.field> syntax

查看:158
本文介绍了如果x在< listOfModels.field>句法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个类型为X的X模型,我的查询返回一个X的列表,
$ b

  {%如果XY在XY%中} 
测试
{%endif%}

编辑: X仍然是一个querySet(我不是迭代集)。



提前感谢

解决方案

你几乎在那里您只需返回一个实际的查询器:

  {%如果在XYall%中有A} 
测试
{%endif%}

更新(基于评论) p>

这是不可能的模板代码,你需要做一个过滤器,Django模板语言不允许传递参数到方法。在您看来,您可以执行以下操作:

  X.objects.filter(Y = A).exists()

将结果传递到模板中使用的上下文,但我不知道如何与您的确切需要。


I have a model X with a ManyToMany field Y, my query returns a list of X's,

How do I do something like:

{% if A in X.Y %}
     Test
{% endif %}

EDIT: X is still a querySet (I'm not iterating the set).

Thanks in advance,

解决方案

You're pretty much there. You just have to return an actual queryset:

{% if A in X.Y.all %}
    Test
{% endif %}

UPDATE (based on comment)

That is not possible with template code, you need to do a filter, and the Django templating language doesn't allow passing parameters to methods. In your view, you can do:

X.objects.filter(Y=A).exists()

And pass the result into the context to be used in the template, but I'm not sure how that fits with your exact needs.

这篇关于如果x在< listOfModels.field>句法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆