Search code examples
djangogoogle-app-enginedjango-nonrel

Simple Django Search Run on Django App Engine?


In my Django project that aggregates NBA player stats (as a learning project), I've built a simple search view that allows you to search for players by first or last name (or both). It works totally fine on the dev server, but now I want to deploy my site using Google App Engine, and I'm getting errors that I believe are due to GAE's High Replication Datastore.

I was under the impression that the Django-nonrel/dbindexer tools would help alleviate this problem, but so far I've tried implementing them to no avail--when I attempt a search on my site, I get an error log with the message: 'DatabaseError: This query is not supported by the database.'

I'm guessing my problem might be that the instructions for these tools are spread across multiple docs written at different points in their development, so it's tricky for me to put them together into one coherent picture. Also, I've looked at the testapp they offer and it seems to be set up somewhat differently than their instructions would suggest. So I'd like to ask two questions:

1) Is the view I've coded is even possible to use with GAE and Django-nonrel/dbindexer? Here it is:

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) If it is possible to use, and my project in dev is structured as follows, what is the most parsimonious set of steps I can follow to make my search view work properly on 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)

Solution

  • 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.