【发布时间】:2014-10-02 20:02:56
【问题描述】:
我有一个 Story 模型,它通过 django-tagging 应用程序包含一个 TagField。 我正在获取特定标签的故事列表。在功能上我的代码运行良好,但我遇到了一个我不太理解的行为。
以下代码正确地为我提供了所需的故事列表。
ct = ContentType.objects.get_for_model(Story)
ti = TaggedItem.objects.filter(tag__name='mobile',content_type=ct).values_list('object_id',flat=True)
stories = Story.published_objects.filter(id__in=ti)
但是,我只想要前 50 个故事。所以我做了什么:
ct = ContentType.objects.get_for_model(Story)
ti = TaggedItem.objects.filter(tag__name='mobile',content_type=ct).values_list('object_id',flat=True)
ti50 = ti[:50]
stories = Story.published_objects.filter(id__in=ti50)
在这里,我将列表本身切片为仅包含 50 个 ID,然后将切片列表提供给 __in 子句。所以我期待列表中有 50 个故事。但是,我得到了这个错误:
DatabaseError: (1235, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'")
我不明白为什么在 MySQL 中使用 LIMIT,尽管我试图在 Python 本身中预先对其进行切片。
对此有何解释?
【问题讨论】:
标签: python mysql django django-queryset