I implemented it as follows, overriding the put method and using key.name
class Program(db.Model):
name = db.StringProperty("Title")
slug = db.StringProperty("Slug")
def put(self):
if Program.get_by_key_name(self.slug):
raise UniqueConstraintViolation("slug", self.slug)
self._key_name = self.slug
return db.Model.put(self)
class UniqueConstraintViolation(Exception):
def __init__(self, scope, value):
super(UniqueConstraintViolation, self).__init__("Value '%s' is not unique within scope '%s'." % (value, scope, ))
I save slug as key.name, and if you try to add another program, it checks if an existing key name already exists. This is probably not a very good way, I am also a beginner in the python / app engine.
This is a good article about someone using a helper model: http://squeeville.com/2009/01/30/add-a-unique-constraint-to-google-app-engine/
Edit: I saw that you also provided this article lol.
source
share