【问题标题】:Django nonrel Query confusionDjango nonrel 查询混淆
【发布时间】:2016-01-19 22:06:38
【问题描述】:

我正在创建一个托管在 Google AppEngine 上的练习 django-nonrel 项目,以熟悉 Django 和 AppEngine 平台,但遇到了一个令人困惑的查询问题。这是我的models.py:

  class Character(models.Model):
        name = models.CharField(max_length=200)
        url_name = models.CharField(max_length=200)

        def save(self, *args, **kwargs):
            self.url_name = self.name.replace(" ", "-").lower()
            super(Character, self).save(*args, **kwargs) # Call the "real" save() method.


    def __unicode__(self):
        return '%s' %(self.name)

class Season(models.Model):
    season_number = models.IntegerField()

    def __unicode__(self):
        return u'Season %d' % (self.season_number)

class Episode(models.Model):
    season = models.ForeignKey(Season)
    episode_name = models.CharField(max_length=200)
    episode_number= models.IntegerField()

    def __unicode__(self):
        return u'Season %d, Episode %d "%s"' %(self.season.season_number, self.episode_number, self.episode_name)

class Quote(models.Model):
    author = models.ForeignKey(Character)
    episode = models.ForeignKey(Episode)
    quote_text = models.TextField()

    def __unicode__(self):
        return u'"%s" (%s)' % (self.quote_text, self.episode)

现在这里是混乱的地方:为什么 Quote.objects.all().order_by('episode') 有效,而 Quote.objects.filter(author__name="Michael Scott").order_by('episode')不行?正如最后一个过滤器所证明的那样,问题不在于 author__name 调用,因为该索引已使用 dbindexer 注册。问题出在 order_by('episode') 部分,但我不明白为什么这会在一个 QuerySet 而不是另一个 QuerySet 上工作。

>>> from QuotesCMS.models import Character, Season, Episode, Quote
>>> Quote.objects.all().order_by('episode')
[<Quote: "My proudest moment here wasn't when I increased profits by 17 percent, or cut expenditure without losing a single member of staff. No no no. It was a young Guatemalan guy, first job in the country, barely spoke a word of English, but he came to me and said, 'Mr. Scott, will you be the godf
ather to my child?' Wow... wow. Didn't work out in the end. We had to let him go. He sucked." (Season 1, Episode 1 "Pilot")>, <Quote: " Abraham Lincoln once said, 'If you are a racist, I will attack you with the North,' and those are the principles I carry with me into the workplace." (Season 1, Epi
sode 2 "Diversity Day")>, <Quote: "I love inside jokes... I'd love to be a part of one someday." (Season 3, Episode 2 "The Convention")>, <Quote: "I declare...bankruptcy!!!" (Season 4, Episode 4 "Money")>, <Quote: "When I'm at home at night in my own house in my sweats, drinking red wine, watching m
y mystery stories the last thing in the whole godforsaken world I want to hear is the voice of Michael Scott." (Season 4, Episode 4 "Money")>, <Quote: "As of this morning, we are completely wireless here on Schrute Farms. So as soon as I find out where Mose hid all the wires, we can have power back
on. " (Season 4, Episode 4 "Money")>, <Quote: "I just realized that this is Pam's and my first night away together. I used to play it over in my head, and it was just a little bit different. Maybe a nice hotel, or a romantic dinner, wine, but wine that wasn't made out of beets. Didn't think Dwight w
ould be involved at all. And I always imagined less manure. I mean, some manure, just less." (Season 4, Episode 4 "Money")>]

-

>>> Quote.objects.filter(author__name="Michael Scott").order_by('episode')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\OfficeQuotes\django\db\models\query.py", line 93, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\OfficeQuotes\django\db\models\query.py", line 108, in __len__
    self._result_cache.extend(self._iter)
  File "C:\OfficeQuotes\django\db\models\query.py", line 317, in iterator
    for row in compiler.results_iter():
  File "C:\OfficeQuotes\djangotoolbox\db\basecompiler.py", line 411, in results_iter
    for entity in results:
  File "C:\OfficeQuotes\djangoappengine\db\compiler.py", line 138, in fetch
    for entity in results:
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_query.py", line 3317, in next
    next_batch = self.__batcher.next_batch(Batcher.AT_LEAST_OFFSET)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_query.py", line 3203, in next_batch
    batch = self.__next_batch.get_result()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 613, in get_result
    return self.__get_result_hook(self)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_query.py", line 2917, in __query_result_hook
    yaml_index=yaml, xml_index=xml)
NeedIndexError: no matching index found.
The suggested index for this query is:
- kind: QuotesCMS_quote
  properties:
  - name: author_id
  - name: episode_id

-

 >>> Quote.objects.filter(author__name="Michael Scott")
    [<Quote: "I declare...bankruptcy!!!" (Season 4, Episode 4 "Money")>, <Quote: " Abraham Lincoln once said, 'If you are a racist, I will attack you with the North,' and those are the principles I carry with me into the workplace." (Season 1, Episode 2 "Diversity Day")>, <Quote: "My proudest moment her
    e wasn't when I increased profits by 17 percent, or cut expenditure without losing a single member of staff. No no no. It was a young Guatemalan guy, first job in the country, barely spoke a word of English, but he came to me and said, 'Mr. Scott, will you be the godfather to my child?' Wow... wow.
    Didn't work out in the end. We had to let him go. He sucked." (Season 1, Episode 1 "Pilot")>, <Quote: "I love inside jokes... I'd love to be a part of one someday." (Season 3, Episode 2 "The Convention")>]

希望这些报价有点有趣,并在此先感谢您的帮助。

【问题讨论】:

    标签: python django google-app-engine google-cloud-datastore django-nonrel


    【解决方案1】:

    您需要管理和更新应用查询的索引。

    检查(至少)Datastore IndexesIndex configuration 部分。

    您收到的错误消息还包括您执行的特定查询可能需要的索引信息。

    另请注意,索引配置在上传后立即生效,可能需要一段时间才能构建新添加的索引。我只看到最多几分钟的延迟,但我的数据库也很小。 YMMV。

    【讨论】:

    • 我的理解是 djangoappengine 和 dbindexer 负责索引。我一直在运行大量查询而没有触及 index.yaml 文件。我的印象是我不必弄乱默认的 index.yaml 文件,所以我没有考虑检查给定的文章。显然我错了,哈哈。再次感谢 Dan 帮助像我这样的新手找到自己的路。
    猜你喜欢
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2014-07-02
    相关资源
    最近更新 更多