我有一個用例,我必須計算ManyToManyField的出現次數,但它比我想象的要複雜得多。ManytoManyField的反向計數條件
models.py:他們的整體,但只對那些People
誰擁有> 0和< 6標籤出現
class Tag(models.Model):
name = models.CharField(max_length=100, unique=True)
class People(models.Model):
tag = models.ManyToManyField(Tag, blank=True)
在這裏,我不得不拿出Tags
列表的數量和時間。喜歡的東西:
tag1 - 265338
tag2 - 4649303
tag3 - 36636
...
這是我想出了最初的計數:
q = People.objects.annotate(tag_count=Count('tag')).filter(tag_count__lte=6, tag_count__gt=0)
for tag in Tag.objects.all():
cnt = q.filter(tag__name=tag.name).count()
# doing something with the cnt
但後來我意識到,這可能是低效的,因爲我可能通過People
模型多次迭代(記錄在人中比在標籤中的人大)。
直覺上我認爲我應該能夠做一個Tag
模型的迭代而不需要任何迭代People
模型。所以然後我想出了這個:
for tag in Tag.objects.all():
cnt = tag.people_set.annotate(tag_count=Count('tag')).filter(tag_count__lte=6).count()
# doing something with the cnt
但是,首先,這不會產生預期的結果。其次,我認爲這似乎變得更加複雜,所以也許我會使一件簡單的事情變得複雜。歡迎任何建議。
更新:我得到了queryset.query並在db上運行查詢來調試它。出於某種原因,結果連接中的tag_count列顯示全部爲1。似乎無法理解爲什麼。
花了很多時間在這個,但無法弄清楚爲什麼上面的第二種方法不起作用。任何人? – Anupam