如何比较输入字符串与数据库对象

时间:2014-05-12 11:35:43

标签: django

当我将feature_name的输入作为newFeature时,它会给我找到文件夹但是当我给newFeature123时,它会给我找不到文件夹

在FeatureCache表中,这两个是newFeature123newFeature。这些文件夹名称存在,但它与newFeature匹配,但与newFeature123不匹配。

views.py:

def passReviewerProjFeature(request):
    if request.method == 'POST':
        feature_name = request.POST['tcrsearch_id']
        project_id = request.POST['tcrproject_id']
        featureObjectarray=FeatureCache.objects.filter(project=project_id)
        for object in featureObjectarray:
               print object.name
               if feature_name not in str(object.name):
                   print feature_name
                   print "not Present"
                   return render(request,'index.html',{'errmsg':"Folder Not Found"})
               else:
                   return render(request,'index.html',{'errmsg':"Folder Found"})

1 个答案:

答案 0 :(得分:1)

在django queryset中查找field lookups

基本上containsicontains可能就是您要找的地方:

FeatureCache.objects.filter(project__icontains=feature_name, project__exact=project_id)

将执行不区分大小写的查找。像这样:

featureObjectarray=FeatureCache.objects.filter(project__icontains=feature_name, project__exact=project_id)
if featureObjectarray.exists():
  return render(request,'index.html',{'errmsg':"Folder Found"})
else:
  print feature_name
  print "not Present"
  return render(request,'index.html',{'errmsg':"Folder Not Found"})