开发者

Google App Engine db.key() question / User-Feed model

开发者 https://www.devze.com 2022-12-16 12:46 出处:网络
am designing a feed system for users. so i have: class User(db.Model): name = db.StringProperty() list_of_keys = db.StringListProperty()

am designing a feed system for users.

so i have:

class User(db.Model):
  name = db.StringProperty()
  list_of_keys = db.StringListProperty()

class Feed(db.Model):
  event = db.StringProperty()
  timestamp = db.DateTimeProperty()

# I created 10 feed entities

# assuming i already have my 10 feed entities, I take their keys and put into this list
list_of_keys = ["key1","key2","key3",.........."key10"]

if I use

db.key(list_of_keys)

I get all the entities. no problem. So it works when all of the keys are present ( ie: it works if the entities are still in the datastore)

But if I delete even one of the开发者_运维技巧 entities, then db.key() returns a NoneType.

any suggestions on how to go about this?

PS: i'm trying to avoid using ReferenceProperty in my feed system, cos I think it might get messy

if you still suggest that I use ReferenceProperty, here are my goal(s):

1. Each feed entity should "belong" to more than one user.

2. Each user can have up to 20 feeds max.

3. New feeds will replace the old ones based on timestamp.

would love to hear your thoughts/solutions.


Assuming you have the strings that encode the actual keys (as opposed to just the keynames or something else again), first make the list of strings (that you so peculiarly and confusingly choose to call instead a list of keys though it's clearly and obviously a list of strings instead!) back into a list of Keys:

thekeysforreal = [db.Key(k) for k in list_of_keys]

Now, db.get(thekeysforreal) will do its job, per the docs:

If a list of Keys is provided, the return value is a corresponding list of model instances, with None values when no entity exists for a corresponding Key.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号