0
比方說,爲簡單起見,我有三大類:的Django/Postgres的:顯示不同的多對多項目
class BigThing(models.Model):
title = models.CharField(max_length=48)
stufflists = models.ManyToManyField(StuffList)
class StuffList(models.Model):
title = models.CharField(max_length=48)
stuff = models.ManyToManyField(Stuff)
class Stuff(models.Model):
code = models.CharField(max_length=24)
title = models.CharField(max_length=48)
description = RedactorField(blank=True, null=True)
BigThing1包含列表1和項目list3。
List1包含Item1,Item2,Item3。
List2包含Item1,Item2和Item4。
List3只包含Item3。
我想返回所有屬於BigThing1,省略重複,像這樣的項目:
Item1, Item2, Item3
NOT: Item1, Item2, Item3, Item3
(and obviously not Item4, since that doesn't belong to either of the lists associated with BigThing1)
我覺得有涉及prefetch_related或select_related東西,但也有點爲難,因爲我新的Django/Postgres。在過去,我會做一個JOIN查詢,但承認這些查詢無論如何都是黑客,無論如何,我想知道這種查詢的最佳做法。
這就是它!非常感謝你,我不知道那裏有什麼心理障礙。 – ingernet