Archive

Archive for the ‘appengine’ Category

appengine – SELECT * From my_model WHERE category LIKE ‘news%’

December 13, 2011 Leave a comment

Problem:
I needed to use a LIKE query in appengine.

Solution:
Got some great answers from google search.

Source:
The solution I went with is here.
http://pythoughts.com/app-engine-datastore-workarounds-and-search/

From pythoughts.com

Where in sql you usually would query:

"SELECT * FROM thought WHERE user LIKE gra%"

We now have to use:

"SELECT * FROM thought WHERE user >= 'gra' AND user < :1", u'gra' + u'\ufffd'"

Imagine the table as being sorted alphabetically, user >= ‘gra’ would include any user-names that come after, or equal, ‘G’, then ‘R’, then ‘A’ in the alphabet. The next filter is a less-than inequality, so it’s going to filter out any values that are greater than the specified term, which we set as u’gra’ + u’\ufffd’. The unicode character and ‘\ufffd’ code specify that the result must include the first three character, ‘gra’ (formatted as unicode in order to concatenate with “u’\ufff'”), as well as any other unicode characters following that. This will limit the query enough to return a value such as ‘graeme’, if the search term is ‘grae’, but will not return ‘graham’, or, say, ‘tagrae’.

NB.
1. This works pretty well as the amount of data that I have will be “relatively small”.
2. The data saved in category field is in lowercase, without spaces or punctuation for easier search. Above solution is case sensitive.

Advertisements
Categories: appengine Tags:

Appengine – Appstats (python)

September 15, 2011 Leave a comment

Setting up Appstats for python and djangoappengine.

Assuming you have your djangoappengine setup as explained here.
Now following the instructions from http://code.google.com/appengine/docs/python/tools/appstats.html

1. Add this to settings.py

MIDDLEWARE_CLASSES = (
    'google.appengine.ext.appstats.recording.AppStatsDjangoMiddleware',

    # ...
)

2. Add this to app.yaml

builtins:
- appstats: on

That’s it. Now start your server is running

python2.5 manage.py runserver

Point your browser to http://127.0.0.1:8000/_ah/stats/
You should see something like
Appstats

Now add some data by visiting some pages eg http://127.0.0.1:8000/ (Do this in a different tab then refresh.) You should have some stats now.
Appstats

That’s it. Very simple and straight forward instructions from the docs.

Categories: appengine, django Tags: ,

Google I/O: Building Scalable, Complex Apps on App Engine

September 15, 2011 Leave a comment

appengine – SELECT using OR clause – alternative

September 15, 2011 Leave a comment

Problem:
Appengine does not support SELECT using an OR clause. There are a number of options out there and this is one of them. The Kind I am using this on will not have alot of data and I am hoping (fingers crossed) that it will not cause a performance issue later on.

Solution:
Filter, StringListProperty.

from googleappengine.ext import db

class Post(db.Model):
    author = db.StringProperty()
    category = db.StringListProperty()

post = Post(author="Sam", category=['bash', 'emacs','lisp'])
post.save()
post = Post(author="Peter", category=['bash', 'appengine'])
post.save()
post = Post(author="Ken", category=['mail', 'django','irc', 'sql'])
post.save()

results = Post.all().filter('category = ', 'bash')

for item in results:
    print item.author, item.category

Results
You would then get the following two items.

Sam [u'bash', u'emacs', u'lisp']
Peter [u'bash', u'appengine']

Source:
http://stackoverflow.com/questions/3455090/querying-for-a-value-existing-in-a-models-list-property-in-appengine/7430151#7430151

Categories: appengine