There is no reason to store uid twice if it is equal to key name minus a prefix. You uid will just be user.key().name()[4:] If you change uid in datastore, the get_or_insert_by_uid will stop working as well.
On Oct 25, 12:30 pm, Alexis Bellido <[EMAIL PROTECTED]> wrote: > Hello everybody, I needed to store some information about my users (I > don't need to use Google accounts) and created a model called FbUser, > each entity in the model is a fbuser and each fbuser has a unique user > uid, a field I call 'uid'. > > A uid can't appear more than once in the datastore but it could > eventually change for a user, so it's not inmutable. It can't be used > directly as a key_name because it always starts with a number, in fact > is always a ten digit number. > > I want to make sure that I only store unique uid's in the datastore so > I thought about Model.get_or_insert: > > http://code.google.com/appengine/docs/datastore/modelclass.html#Model... > > and found this suggestion by Dado (thanks a lot for it): > > http://groups.google.com/group/google-appengine/browse_thread/thread/... > > I implemented it for my case like this: > > class FbUser(db.Model): > """ > You can then call FbUser.get_or_insert_by_uid('foo') and get back an > FbUser instance with that unique identifier (it gets created if it > does not yet exists).Model.get_or_insert is automatically wrapped in > a transaction > """ > > uid = db.StringProperty(required=True) > > @staticmethod > def get_or_insert_by_uid(uid): > # prefix with unique identifier to qualify and avoid beginning > with digits, which datastore does not accept > key_name = 'uid:'+uid > return FbUser.get_or_insert(key_name, uid=uid) > > And then I can 'get or create' a fbuser with uid '123' using this: > > FbUser.get_or_insert_by_uid('123') > > I've tested and it works but I want to be sure if this is the right > way of doing it. What do you think? > > Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~----------~----~----~----~------~----~------~--~---
