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_get_or_insert

and found this suggestion by Dado (thanks a lot for it):

http://groups.google.com/group/google-appengine/browse_thread/thread/9f956ebcc39dd80f/bf5bf1edd53a4410?lnk=gst&q=Model.get_or_insert#bf5bf1edd53a4410

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 google-appengine@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to