How to Present One-to-One Relationships in App Engine

Say that you have the concept of โ€œcustomโ€ records that you want to keep in the data warehouse.

class User (db.Model):
  first_name = db.StringProperty()
  last_name = db.StringProperty()
  created = db.DateTimeProperty(auto_now_add=True)
  twitter_oauth_token = db.StringProperty()
  twitter_oauth_secret = db.StringProperty()

There are several fields that you would like to use almost always when using a custom object, for example first_name and last_name.

However, there are several fields in which you have only one use case, for example twitter_oauth_token and twitter_oauth_secret, and are somewhat inefficient to worry about serializing and deserializing this data when it is not needed in 95% of cases.

So, if you split your model up:

class User (db.Model):
  first_name = db.StringProperty()
  last_name = db.StringProperty()
  created = db.DateTimeProperty(auto_now_add=True)

class UserTwitterOauth(db.Model):
  oauth_token = db.StringProperty(required=True)
  oauth_secret = db.StringProperty(required=True)
  created = db.DateTimeProperty(auto_now_add=True)

ReferenceProperty UserTwitterOauth, --, , UserTwitterOauth . , UserTwitterOauth. " "?

+3
2

UserTwitterOauth User , :

my_user = User(first_name="John", last_name="Smith")
my_user.put()
extra_info = UserTwitterOauth(parent=my_user, key_name="UserTwitterOauth")
extra_info.put()

User, , - UserTwitterOauth factory, .

, , User - API- Users User, , , .

+6

twitter, , . , .

, GAE, - .

EDIT, , :

User.usertwitteroauth_set. , collection_name ReferenceProperty. , , , :

class User(db.Model):
    def set_access_token(self, access_token):
        db.delete(self.twitter_access_tokens) # Think this should work, otherwise iterate over the query.
        new_access_token.user = self
        new_access_token.put()


class UserTwitterOauth(db.Model):
    user = db.ReferenceProperty(User, collection_name = 'twitter_access_tokens')
0

All Articles