django orm __in,但不是精确,而是包含案例不敏感的?
我目前正在尝试使用django orm查询食谱
模型的成分。
class Recipe(models.Model):
account = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
name = models.TextField(null=True, blank=True)
class RecipeIngredients(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, null=True)
ingredient = models.TextField(null=True, blank=True)
到目前为止,我
ingredients = ["eggs", "bacon", "potato"]
recipes = Recipe.objects.filter(
recipeingredients__ingredient__in=ingredients
).alias(
ningredient=Count('recipeingredients')
).filter(
ningredient__gte=len(ingredients)
)
对这个答案这将返回所有仅包含“鸡蛋”,“”,“培根”和“马铃薯”,但不说鸡蛋或炒鸡蛋。无论如何,是否有可以调整此内容以搜索所有包含成分和病例无敏化的项目?
I am currently trying to use the Django ORM to query the Recipe
model's ingredients.
class Recipe(models.Model):
account = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
name = models.TextField(null=True, blank=True)
class RecipeIngredients(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, null=True)
ingredient = models.TextField(null=True, blank=True)
What I have so far is
ingredients = ["eggs", "bacon", "potato"]
recipes = Recipe.objects.filter(
recipeingredients__ingredient__in=ingredients
).alias(
ningredient=Count('recipeingredients')
).filter(
ningredient__gte=len(ingredients)
)
From my understanding of this answer this will return all the items that contain only "eggs", "bacon", and "potato", but not say Eggs or Scrambled EGGS. Is there anyway to adjust this to have it search for all items that contains the ingredients and case insensative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下条件来创建条件的脱节,
一个潜在的问题可能是,如果查询为
eggs
,而两种成分匹配,例如'White Eggs'
和'棕色鸡蛋'
,这些将算作两个,因此,另一种成分,例如bacon
可能不是成分,并且仍然是<代码> QuerySet ,因此不幸的是,制作与所有成分完全匹配的QuerySet
并不容易。潜在的解决方案可能是:
但是,这将使 n 与 n 成分结合在一起,因此对于大量
成分
,很容易变得不可行。You can create a disjunction of conditions, with:
A potential problem might be that if the query is
eggs
, and two ingredients match, like'white eggs'
and'brown eggs'
, these will count as two, and therefore another ingredient, likebacon
might not be an ingredient, and will still be part of theQuerySet
, so unfortunately, it is not easy to make aQuerySet
that matches exactly all ingredients.A potential solution could be:
But this will make n JOINs for n ingredients, and thus can easily become infeasible for a large amount of
ingredients
.