How to create a unique property in python in google engine?

In some database technologies for an attribute in a record, you can guarantee the uniqueness of this attribute throughout the database. An example of this would be the email_address attribute in a user record. By setting a unique email_address address, you guarantee that a specific email address can be displayed in only one entry in the entire database.

Is there any way in the Google engine to create unique properties for this model? For example, can I have a User object (db.Model) with an email property that is guaranteed to be unique throughout the data warehouse?

I found a resource here that may be useful.

+3
source share
3 answers

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.

+2
source

, GAE "", - , , URL-, , . , ( , [[ ]] , ), ( ).

+4

record.key(). id() blogged how,

+2

Source: https://habr.com/ru/post/1724127/


All Articles