简体   繁体   English

检查查询集中的确切存在 django,jinja 2

[英]check for exact presence in a queryset django, jinja 2

In my case, I have a question to check if the exact string name of a model does exist in a query set.就我而言,我有一个问题要检查查询集中是否存在 model 的确切字符串名称。 here is my code:这是我的代码:

views.py:

if Despiking.objects.filter(user=request.user).exists():
    filtered_projects = Despiking.objects.filter(user=request.user)
    context.update({
        'filtered_projects': filtered_projects.__str__(),
    })

template.html:

{% if info.project_name in filtered_projects %}
    <!-- some HTML elements -->
{% else %}
    <!-- other HTML elements -->
{% endif %}

In my code, there is no difference between "my project" and "project" as info.project_name model. because of that the "project" word exists in the query set when I have only the "my project" in it.在我的代码中, "my project""project"之间没有区别,如info.project_name model。因为当我只有"my project"时, "project"一词存在于查询集中。 so using {% if info.project_name in filtered_projects %} works the same (the condition of if will be True ) because that "project" word exists in the query set because of the "my project" .所以使用{% if info.project_name in filtered_projects %}的效果相同( if的条件将为True ),因为由于"my project" ,查询集中存在"project"这个词。 what can I do to check the exact string in it?我该怎么做才能检查其中的确切字符串?

You can use __exact or __iexact for exact matching in case-sensitive and case-insensitive operations respectively.您可以分别在区分大小写和不区分大小写的操作中使用__exact__iexact进行精确匹配。

Share your Despiking model, I'd edit the answer.分享您的Despiking model,我会编辑答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM