django orm __in,但不是精确,而是包含案例不敏感的?

发布于 2025-02-02 23:39:42 字数 848 浏览 1 评论 0原文

我目前正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2025-02-09 23:39:42

您可以使用以下条件来创建条件的脱节,

from django.db.models import Q

ingredients = ['eggs', 'bacon', 'potato']

recipes = Recipe.objects.filter(
    Q(
        *[('recipeingredients__ingredient__icontains', ingredient) for ingredient in ingredients],
        _connector=Q.OR
    )
).alias(
    ningredient=Count('recipeingredients')
).filter(
    ningredient__gte=len(ingredients)
)

一个潜在的问题可能是,如果查询为eggs,而两种成分匹配,例如'White Eggs''棕色鸡蛋',这些将算作两个,因此,另一种成分,例如bacon可能不是成分,并且仍然是<代码> QuerySet ,因此不幸的是,制作与所有成分完全匹配的QuerySet并不容易。

潜在的解决方案可能是:

ingredients = ['eggs', 'bacon', 'potato']

recipes = Recipe.objects.all()

for ingredient in ingredients:
    recipes = recipes.filter(recipeingredients__ingredient__icontains=ingredient)

但是,这将使 n n 成分结合在一起,因此对于大量成分,很容易变得不可行。

You can create a disjunction of conditions, with:

from django.db.models import Q

ingredients = ['eggs', 'bacon', 'potato']

recipes = Recipe.objects.filter(
    Q(
        *[('recipeingredients__ingredient__icontains', ingredient) for ingredient in ingredients],
        _connector=Q.OR
    )
).alias(
    ningredient=Count('recipeingredients')
).filter(
    ningredient__gte=len(ingredients)
)

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, like bacon might not be an ingredient, and will still be part of the QuerySet, so unfortunately, it is not easy to make a QuerySet that matches exactly all ingredients.

A potential solution could be:

ingredients = ['eggs', 'bacon', 'potato']

recipes = Recipe.objects.all()

for ingredient in ingredients:
    recipes = recipes.filter(recipeingredients__ingredient__icontains=ingredient)

But this will make n JOINs for n ingredients, and thus can easily become infeasible for a large amount of ingredients.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文