First off, I'm relatively new to Google App Engine, so I'm probably doing something silly.
Say I've got a model Foo:
class Foo(db.Model):
name = db.StringProperty()
I want to use name
as a unique key for every Foo
object. How is this done?
When I want to get a specific Foo
object, I currently query the datastore for all Foo
objects with the target unique name, but queries are slow (plus it's a pain to ensure that name
is unique when each new Foo
is created).
There's got to be a better way to do this!
Thanks.
Here is a pretty thorough discussion of unqiueness with the AppEngine datastore: How do I define a unique property for a Model in Google App Engine?
I've used the code below in a project before. It will work as long as the field on which you're basing your key name on is required.
You could then modify your example model like so:
Of course, this could be dramatically simplified in your case, changing the first line of the
NamedModel
's__init__
method to something like: