我有三个模型,分别是冰箱,食谱和配料,我想获取所有食谱的查询集,并将它们的配料与冰箱对象的食物含量进行比较。我想过滤掉所有食谱,这些食谱没有与我的食物内容匹配的配料,而仅返回那些匹配的查询集。
冰箱和食物都在一个应用程序中,食谱和配料都在另一个应用程序中。
我对Django来说还很陌生,但是我找不到解决方法。我只设法使用搜索输入过滤掉配方,但这只是一个字。
class Fridge(models.Model):
name = models.CharField(max_length=20)
belongs_to = models.ForeignKey(User, related_name="my_fridge", on_delete=models.CASCADE)
food_contents = models.ManyToManyField(Food, related_name="in_fridges")
slug = models.SlugField(allow_unicode=True, unique=True)
class Food(models.Model):
name = models.CharField(max_length=20, unique=True)
quantity = models.PositiveIntegerField()
description = models.TextField(max_length=255, blank=True)
date_added = models.DateTimeField(default=timezone.now)
expiration_date = models.DateTimeField()
is_expired = models.BooleanField(default=False)
slug = models.SlugField(allow_unicode=True, unique=True)
class Recipe(models.Model):
name = models.CharField(max_length=255, unique=True)
rating = models.CharField(max_length=255)
preparation_time = models.CharField(max_length=255)
servings = models.CharField(max_length=255)
instructions = models.CharField(max_length=1000)
slug = models.SlugField()
class Ingredient(models.Model):
name = models.CharField(max_length=255)
recipe = models.ForeignKey(Recipe, related_name="ingredients", on_delete=models.CASCADE)