I am using appengine with python 2.7 and webapp2 framework. I am not using ndb.model.
I have the following model:
class Story(db.Model);
name = db.StringProperty()
class UserProfile(db.Model):
name = db.StringProperty()
user = db.UserProperty()
class Tracking(db.Model):
user_profile = db.ReferenceProperty(UserProfile)
story = db.ReferenceProperty(Story)
upvoted = db.BooleanProperty()
flagged = db.BoolenProperty()
A user can upvote and/or flag a story but only once. Hence I came up with the above model.
Now when a user clicks on the upvote
link, on the database I try to see if the user has not already voted it, hence I do try to do the following:
get the user instance with his id as
up = db.get(db.Key.from_path('UserProfile', uid))
then get the story instance as follows
s_ins = db.get(db.Key.from_path('Story', uid))
Now it is the turn to check if a
Tracking
based on these two exist, if yes then don't allow voting, else allow him to vote and update the Tracking instance.
What is the most convenient way to fetch a Tracking
instance given an id(db.key().id()
) of user_profile
and story
?
What is the most convenient way to save a Tracking
model having given a user profile id and an story id?
Is there a better way to implement tracking?