1

在我的聚合 NBA 球员统计数据的 Django 项目中(作为一个学习项目),我构建了一个简单的搜索视图,允许您按名字或姓氏(或两者)搜索球员。它在开发服务器上运行良好,但现在我想使用 Google App Engine 部署我的网站,并且我收到了我认为是由于 GAE 的高复制数据存储区造成的错误。

我的印象是Django-nonrel/dbindexer工具将有助于缓解这个问题,但到目前为止,我已经尝试实施它们无济于事——当我尝试在我的网站上搜索时,我收到一条带有消息的错误日志:'DatabaseError:数据库不支持此查询。'

我猜我的问题可能是这些工具的说明分散在多个文档中,这些文档是在其开发的不同阶段编写的,因此我很难将它们组合成一张连贯的图片。另外,我查看了他们提供的 testapp,它的设置似乎与他们的说明有所不同。所以我想问两个问题:

1) 我编写的视图是否甚至可以与 GAE 和 Django-nonrel/dbindexer 一起使用?这里是:

def search(request):
        query = request.GET.get('q','')
        querywords = query.split(' ')
        lname=''
        for word in querywords[1:-1]:
                lname += word+' '
        lname += querywords[-1]
        if query:
                if len(querywords)>1:
                        qset = (
                                Q(first_name__iexact=querywords[0]) &
                                Q(last_name__iexact=lname) 
                        )
                        exact_results = Player.objects.filter(qset).distinct()
                        if exact_results != []:
                                result = exact_results
                                qset = (
                                        Q(first_name__icontains=querywords[0])|
                                        Q(last_name__icontains=querywords[0])|
                                        Q(first_name__icontains=lname)|
                                        Q(last_name__icontains=lname)   
                                )
                                results = Player.objects.filter(qset).distinct()
                                results = results.order_by('last_name','first_name')
                        else:
                                qset = (
                                        Q(first_name__icontains=querywords[0]) &
                                        Q(last_name__icontains=lname)|
                                        Q(first_name__icontains=querywords[0])|
                                        Q(last_name__icontains=querywords[0])
                                )
                                results = Player.objects.filter(qset).distinct()
                                results = results.order_by('last_name','first_name')
                                result = ''
                else:
                        qset = (
                                Q(first_name__icontains=querywords[0])|
                                Q(last_name__icontains=querywords[0])   
                        )
                        results = Player.objects.filter(qset).distinct()
                        results = results.order_by('last_name','first_name')
                        result = ''
        else:
                results = []
                result = ''
        return render(request,'search.html', {
                'result':result,
                'results': results,
                'query': query,
                'querywords':querywords
        })

2)如果可以使用,并且我在 dev 中的项目结构如下,我可以遵循哪些最简洁的步骤来使我的搜索视图在 GAE 上正常工作?

NBA/
    __init__.py
    manage.py
    settings.py
    urls.py
    templates/
            (html files)
    players/
            __init__.py
            admin.py
            models.py
            tests.py
            views.py
            fixtures/
                    (some .yaml fixture files)
            static/
                    (css, js, and gif files)
4

2 回答 2

2

通常,不支持 JOIN,因此所有带有 OR 的 Q 对象可能都不起作用。

I haven't used dbindexer much, so I'm not sure how capable it is. I do think if you use it, it will make your datastore queries expensive with all the extra indexing it tries to do automatically.

Even if you're using django-nonrel, you can't expect to port a django app over directly if it uses relational data. You'll have to restructure your data and queries to be non-relational.

于 2012-07-19T05:07:43.980 回答
0

Ok, so after fiddling around with different kinds of queries using "python manage.py remote shell", I've come to the following conclusions:

1) As @dragonx mentioned, Q objects with "OR" are not supported.

2) However, Q objects with "&" definitely are.

3) The problem with my search view code, other than the "OR" queries, was the addition of "distinct()" in my "Player.objects.filter()" call. Apparently the HRD doesn't support "distinct()", and this was responsible for the cryptic

"DatabaseError: This query is not supported by the database"

message. By contrast, attempting to use a Q object with "OR" gives a much more helpful

"DatabaseError: Only AND filters are supported"

Now that I've removed the "distinct()" bit from my code (and any "OR"-containing Q objects), the search works fine.

于 2012-07-23T05:30:38.723 回答