Django Q和查找-在复杂查找中查询多个条目
所以我试图在我的项目中实现一个AJAX搜索表单 我的模型包含一个菜谱类和一个配料类,通过外键连接到菜谱,这样每个菜谱都可以有几个配料 我的数据库中有一个名为omelet的食谱条目,它有两种成分——鸡蛋和牛奶 我的问题是这样的:Django Q和查找-在复杂查找中查询多个条目,django,django-models,django-views,django-queryset,django-q,Django,Django Models,Django Views,Django Queryset,Django Q,所以我试图在我的项目中实现一个AJAX搜索表单 我的模型包含一个菜谱类和一个配料类,通过外键连接到菜谱,这样每个菜谱都可以有几个配料 我的数据库中有一个名为omelet的食谱条目,它有两种成分——鸡蛋和牛奶 我的问题是这样的: for title_ing in search_text: recipe_list_search = recipe_list_search & (Q(ingredients__ingredient__icontains=title_ing)) 当分别搜索
for title_ing in search_text:
recipe_list_search = recipe_list_search & (Q(ingredients__ingredient__icontains=title_ing))
当分别搜索每种成分时,我会得到正确的返回值,例如,我的鸡蛋输出:
egg
(AND: ('ingredients__ingredient__icontains', 'egg'))
<QuerySet [<Recipe: Eggs>, <Recipe: omlet>, <Recipe: Pan Egg>]>
以及以下观点:
def search(request):
if request.method == "POST":
search_text = request.POST['search_text']
else:
search_text = ''
recipe_list_search = ''
print(search_text)
search_text = search_text.split()
recipe_list_search = Q()
for title_ing in search_text:
recipe_list_search = recipe_list_search & (Q(ingredients__ingredient__icontains=title_ing))
print(recipe_list_search)
f_search=Recipe.objects.filter(recipe_list_search).distinct()
print(f_search)
context = {'recipe_list_search': f_search}
return render(request, 'stage9/ajax_search.html', context)
可能是一个noob问题,但我非常感谢您的帮助,谢谢。不要在QuerySet上使用AND运算符,尝试使用itertools进行链接
from itertools import chain
your_queryset = #("whatever_you_queryset_is") ie, Q(ingredients__ingredient__icontains=title_ing)
for title_ing in search_text:
recipe_list_search = list(chain(recipe_list_search, your_queryset))
这可能会使您的代码有一些意义。我没有删除,只是编辑了标题。什么是输出?在你说出你需要什么之前,很难纠正代码对不起,我的不好,但是你的编辑并没有改善这个问题。我正在寻找煎蛋卷条目,这样输出将是蛋奶(和:(“配料”、“配料”、“鸡蛋”)(和:(“配料”、“配料”、“鸡蛋”),(“配料”、“鸡蛋”),(“配料”、“牛奶”)好的,所以chain将提供与使用or运算符“|”相同的效果,这意味着输入的“蛋奶”将返回所有以“奶”或“蛋”为成分的配方。我想要和,所以它将只返回那些同时具有这两个属性的食谱。
class Ingredient(models.Model):
recipe = models.ForeignKey("Recipe", verbose_name=_("Recipe"), related_name="ingredients", on_delete=models.CASCADE)
quantity = models.CharField(_("Quantity"), max_length=10, blank=True, null=True)
unit = models.CharField(_("Unit"), choices=fields.UNITS, blank=True, null=True, max_length=20)
ingredient = models.CharField(_("Ingredient"), max_length=100)
note = models.CharField(_("Note"), max_length=200, blank=True, null=True)
def __str__(self):
_ingredient = '%s' % self.ingredient
if self.unit:
_ingredient = '%s %s' % (self.get_unit_display(), _ingredient)
if self.quantity:
_ingredient = '%s %s' % (self.quantity, _ingredient)
#python anywhere doesnt register len for somereason so should be changed
if len(self.note):
_ingredient = '%s - %s' % (_ingredient, self.note)
return _ingredient
class Meta:
verbose_name = _("Ingredient")
verbose_name_plural = _("Ingredients")
def search(request):
if request.method == "POST":
search_text = request.POST['search_text']
else:
search_text = ''
recipe_list_search = ''
print(search_text)
search_text = search_text.split()
recipe_list_search = Q()
for title_ing in search_text:
recipe_list_search = recipe_list_search & (Q(ingredients__ingredient__icontains=title_ing))
print(recipe_list_search)
f_search=Recipe.objects.filter(recipe_list_search).distinct()
print(f_search)
context = {'recipe_list_search': f_search}
return render(request, 'stage9/ajax_search.html', context)
from itertools import chain
your_queryset = #("whatever_you_queryset_is") ie, Q(ingredients__ingredient__icontains=title_ing)
for title_ing in search_text:
recipe_list_search = list(chain(recipe_list_search, your_queryset))