我有兩個查詢集,我想進行連接,但我不知道如何繼續。 我要在步驟在Django的查詢集中加入以過濾它
# Example:
# Main queryset
q = Recipe.objects.all()
ingredients = ['salt','oil','flour','tomato']
# This result in all the ingredients necessary
q1 = q.annotate(total=Count('steps__ingredients__ingredient', distinct=True))
# q1 = [{id:1, name: 'salad', total: 5}, {id:2, name: 'pasta', total: 4}]
# This result in the available ingredients.
# First I filtered the ingredients I have, them I counted them
q2 = q.filter(steps__ingredients__ingredient__name__in=ingredients)
.annotate(available=Count('steps__ingredients__ingredient',distinct=True))
# q2 = [{id:1, name: 'salad', available: 3}, {id:2, name: 'pasta', available: 4}]
我想用.filter(total=F('sum'))
在加入兩個結果和過濾器基於每個查詢集創建了兩個註釋
模型
過濾數據class Recipe(models.Model):
name = models.CharField(max_length=50)
steps = models.ManyToManyField(StepRecipe)
class StepRecipe(models.Model):
ingredients = models.ManyToManyField(RecipeIngredient)
class RecipeIngredient(models.Model):
ingredient = models.ForeignKey(Ingredient)
class Ingredient(models.Model):
name = models.CharField(max_length=50)
查詢集結束。 過濾後,這將導致僅顯示ID 2的對象,因爲total = 4和available = 4。
我知道我有可用的|
運算符。我不得不做一些在這一行:
result = q1 | q2
但每當我做到這一點,註釋的一個消失(最後一個,在這種情況下,第二季度的註釋是available
)。有任何想法嗎?如果數據庫可以這樣做,我不想迭代它們。
奇怪的是,有些時候,當我把所有東西放在一行時,它會經常給我預期的結果。我有這樣的印象,這可能是一個錯誤。我認爲它在某種程度上緩存結果...
這是我做的,它導致正確幾次:
queryset = Recipe.objects.all()
.annotate(total=Count('steps__ingredients', distinct=True))
.filter(steps__ingredients__ingredient__name__in=ingredients)
.annotate(sum=Count('steps__ingredients', distinct=True))
.filter(total=F('sum'))
我使用MySQL 5.6和Django的1.11
我試着使用Django 1.11中新增的.intersection()
,它也不起作用(我的數據庫顯然不支持)。我想過使用.union()
,但發生在運算符|
上的同樣的事情發生在這個函數上:一個註釋消失了。儘管一個人消失了,我想我可以看到兩次出現並過濾出來的東西。但是......再次出現另一個錯誤!當我試圖註釋我得到IndexError: list index out of range
如果有一個更好的主意(而不是迭代)或一個好方法來解決這個不涉及原始查詢,我會很感激。謝謝!
目前還不清楚你想達到的目標。請發佈您的模型並解釋您嘗試獲取的數據。 https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – e4c5
你不加入Django,Django爲你加入。換句話說:如果你用SQL思考,你將無法從Django ORM中獲得最好的結果。 –
我添加了一個可見的示例和模型。我知道Django抽象了連接,它被稱爲導航。這只是解釋問題的一種方式 – mk2