Set key from a class variable in google app engine
I have this class and i want to use the phone as key. The key is fron a form input task.phone = self.request.get('phone'). I searched for answer but the on开发者_JS百科ly that i found it was from inside the code (like task=(phone = 5555) ).
class task(db.Model):
name = db.StringProperty()
lanme = db.StringProperty()
phone = db.StringProperty()
adress = db.StringProperty()
visits = db.IntegerProperty()
a simple solution would be:
task = task(key_name=adress)
A more advanced one would be:
class task(db.Model):
...
def __init__(self, *args, **kwargs):
kwargs['key_name'] = kwargs['adress']
db.Model.__init__(self, *args, **kwargs)
But what you need to keep in mind is that appengine doesn't use a particular field as key, there will always be a key and that may or may not match a particular field. If they are the same, is a "coincidence". As a general suggestion, leave the key as it is. There are some issues you may not be considering such as uniqueness of keys and overriding them.
Couple of side notes:
- Class name should be Task not task
- It's address not adress
精彩评论