Django annotate() 不是将条目数相加,而是重复它们

发布于 2025-01-10 06:41:46 字数 2323 浏览 0 评论 0原文

背景

Amazon Kindle PaperWhite 存储我们在读入名为 vocab.db 的 sqlite3 数据库时查找的单词。我正在开发一个小型 Kindle 配套应用程序,该应用程序获取此 db 文件并将其导入 django 表中以进行各种处理。我已经完成了这一步。

我想要做什么:

我想在我的表 KindleLookups 中查询我的最难的单词(例如:我看了多少次特定的单词)。我最终希望将这些数据呈现在按最高计数排序的表格中。

所需结果:

单词查找计数
Reverberated3
Troubadour1
Corrugated1

我的结果(不需要):

这里 Reverberated 重复查找 3 次计数一,而不是一次查找计数三。

单词查找计数
Reverberated1
Reverberated1
Reverberated1
Troubadour1
Corrugated1

模型:

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>

所以我希望你能回答我的问题:

问题

  1. 我的 annotate() 到底发生了什么?尽管我使用了 Count(),但为什么它会重复行而不是对它们进行计数?
  2. 依靠“word”和“word__word”会是完全相同的事情吗?
  3. 这个问题是否与我的 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:

WordLookup Count
Reverberated3
Troubadour1
Corrugated1

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.

WordLookup Count
Reverberated1
Reverberated1
Reverberated1
Troubadour1
Corrugated1

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

  1. What is actually happening with my annotate()? Why is it repeating the rows instead of counting them, despite me using Count()?
  2. Would counting on the "word" and "word__word" be the exact same thing?
  3. Could this issue be somehow related with my KindleLookups's __str__ method returning the self.usage (the sentence the word was found in) and not the word?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作死小能手 2025-01-17 06:41:46

您应该改为注释 KindleWords 模型,因此:

context['lookup_counts'] = KindleWords.objects.annotate(
    word_count=Count('kindlelookups')
)

从此查询集中产生的 KindleWords 将具有一个额外的属性 .word_count,用于确定有多少个单词KindleWordsKindleLookups 中使用的次数。


注意:通常 Django 模型会被赋予一个单一名称,因此 KindleWord 而不是 KindleWords.

You should annotate the KindleWords model instead, so:

context['lookup_counts'] = KindleWords.objects.annotate(
    word_count=Count('kindlelookups')
)

The KindleWordss that arise from this queryset will have an extra attribute .word_count that determines how many times that KindleWords is used in the KindleLookups.


Note: normally a Django model is given a singular name, so KindleWord instead of KindleWords.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文