Django annotate() 不是将条目数相加,而是重复它们
背景:
Amazon Kindle PaperWhite 存储我们在读入名为 vocab.db
的 sqlite3 数据库时查找的单词。我正在开发一个小型 Kindle 配套应用程序,该应用程序获取此 db 文件并将其导入 django 表中以进行各种处理。我已经完成了这一步。
我想要做什么:
我想在我的表 KindleLookups
中查询我的最难的单词(例如:我看了多少次特定的单词)。我最终希望将这些数据呈现在按最高计数排序的表格中。
所需结果:
单词 | 查找计数 |
---|---|
Reverberated | 3 |
Troubadour | 1 |
Corrugated | 1 |
我的结果(不需要):
这里 Reverberated
重复查找 3 次计数一,而不是一次查找计数三。
单词 | 查找计数 |
---|---|
Reverberated | 1 |
Reverberated | 1 |
Reverberated | 1 |
Troubadour | 1 |
Corrugated | 1 |
模型:
class KindleLookups(TimeStampedModel):
book = models.ForeignKey(KindleBookInfo, on_delete=models.CASCADE)
word = models.ForeignKey(KindleWords, on_delete=models.CASCADE)
...
class KindleWords(TimeStampedModel):
word_key = models.CharField(max_length=255, unique=True)
word = models.CharField(max_length=255)
...
我正在尝试使用 annotate(),但这会重复行而不是由于某种原因将它们相加。
context['lookup_counts'] = KindleLookups.objects.annotate(word_count=Count("word"))
然后我认为我需要对实际单词进行注释,但似乎没有任何改变。
context['lookup_counts'] = KindleLookups.objects.annotate(word_count=Count("word__word"))
模板:
<tbody>
{% for word in lookup_counts %}
<tr>
<td>{{ word.word }}</td>
<td>{{ word.word_count }}</td>
</tr>
{% endfor %}
</tbody>
所以我希望你能回答我的问题:
问题
- 我的 annotate() 到底发生了什么?尽管我使用了 Count(),但为什么它会重复行而不是对它们进行计数?
- 依靠“word”和“word__word”会是完全相同的事情吗?
- 这个问题是否与我的
KindleLookups
的__str__
方法返回self.usage
(找到该单词的句子)有关,而不是这个词?
Background:
The Amazon Kindle PaperWhite stores the words we lookup while reading into a sqlite3 database called vocab.db
. I am working on a small kindle companion app that takes this db file and imports it into a django table for various processing. I have done this step already.
What I would like to do:
I would like to query my table KindleLookups
for my most difficult words (ex: how many times have I looked up a specific word). I would ultimately like to present this data in a table ordered by highest count.
Desired Result:
Word | Lookup Count |
---|---|
Reverberated | 3 |
Troubadour | 1 |
Corrugated | 1 |
My result (undesired):
Here Reverberated
is being repeated three times each with a lookup count of one, instead of one time with three lookup count.
Word | Lookup Count |
---|---|
Reverberated | 1 |
Reverberated | 1 |
Reverberated | 1 |
Troubadour | 1 |
Corrugated | 1 |
Model:
class KindleLookups(TimeStampedModel):
book = models.ForeignKey(KindleBookInfo, on_delete=models.CASCADE)
word = models.ForeignKey(KindleWords, on_delete=models.CASCADE)
...
class KindleWords(TimeStampedModel):
word_key = models.CharField(max_length=255, unique=True)
word = models.CharField(max_length=255)
...
I am trying to accomplish this using annotate(), but this is repeating the rows instead of adding them up for some reason.
context['lookup_counts'] = KindleLookups.objects.annotate(word_count=Count("word"))
I then thought that I needed to annotate on the actual word, but nothing seems to have changed.
context['lookup_counts'] = KindleLookups.objects.annotate(word_count=Count("word__word"))
Template:
<tbody>
{% for word in lookup_counts %}
<tr>
<td>{{ word.word }}</td>
<td>{{ word.word_count }}</td>
</tr>
{% endfor %}
</tbody>
So I am hoping you can answer my questions:
Questions
- What is actually happening with my annotate()? Why is it repeating the rows instead of counting them, despite me using Count()?
- Would counting on the "word" and "word__word" be the exact same thing?
- Could this issue be somehow related with my
KindleLookups
's__str__
method returning theself.usage
(the sentence the word was found in) and not the word?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该改为注释
KindleWords
模型,因此:从此查询集中产生的
KindleWords
将具有一个额外的属性.word_count
,用于确定有多少个单词KindleWords
在KindleLookups
中使用的次数。You should annotate the
KindleWords
model instead, so:The
KindleWords
s that arise from this queryset will have an extra attribute.word_count
that determines how many times thatKindleWords
is used in theKindleLookups
.