Hiya, I asked a question earlier regarding using keys which was answered perfectly.
However, now I'm wondering if creating and using keys for my Models is the way to go, I've been told by my iPhone App guy that we need INTs to store in the database. I got my Python code working with Keys and now may have to back track.
I'd be keen to hear your opinion on the whole creating Key Names vs auto generating IDs that my iPhone app can use without any big changes.
Here's my models:
class User(db.Model):
id = db.StringProperty()
name = db.StringProperty()
image = db.BlobProperty(default=None)
class Book(db.Model):
image = db.BlobProperty()
date_added = db.DateTimeProperty(auto_now_add=True)
is_active = db.BooleanProperty(default=True)
class BookUser(db.Model):
book = db.ReferenceProperty(Book)
user = db.ReferenceProperty(User)
is_active = db.BooleanProperty(default=True)
And how I was dealing with them using Keys (below). Every time a User signs in a BookUser record is created if it doesn't exist already. I need to send all this data to an iPhone, with IDs to store in the database. The below looks pretty solid, hands-up who thinks I should tell the iPhone developer to "suck it up" : )
Or can the below be solved in a better way?
### Get the Users Details
user_id = 1
user = User.get_by_key_name(user_id)
user_key = user.key().name()
### Get the latest active Book
book = Book.all().filter('is_active =', True).get()
book_key = book.key().name()
### Get latest Book for this User
bookuser_key_name = "%s:%s" % (str(book_key), str(user_key))
bookuser = BookUser.get_or_insert(bookuser_key_name)
bookuser.card = card
bookuser.user = user
bookuser.put()
You probably want to avoid using just "id" for field names. It could be confused with the data key's id() when coding. http://code.google.com/appengine/docs/python/datastore/keyclass.html
user_id is good choice.
Your "iPhone guy" wanting to use ints is very concerning. You really don't want to depend or use sequential int values for ids. Beside being tricky to get correct in clustered environments, it can introduce security issues where ids become easily guessable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With