0

I have the following model in python Google App Engine:

class UserAction(ndb.Model):

    author_gae_key = ndb.KeyProperty(indexed=True)
    target_gae_key = ndb.KeyProperty(indexed=True) # key of word, topic, etc.
    action_type = ndb.StringProperty(indexed=True) # select, like, dislike, wantToSee etc.
    recipients_keys = ndb.KeyProperty(repeated=True,indexed=True)

This class is kind of a log, and lets me query relationships such as who clicked on what. Receipients is a list, for people potentially interested by this action (i.e. your friends). This could potentially be long, and I'd rather not serialize it too often.

I have the following function, that tells me if a certain author did a certain action on a certain target:

def isActionDictMultipleTargets(action_type,author_gae_key,target_gae_keys_list):
    out_dict = {}
    if(target_gae_keys_list):
        actions = ndb.gql("SELECT * FROM UserAction WHERE author_gae_key = :1 AND action_type = :2 AND target_gae_key IN :3",author_gae_key,action_type,target_gae_keys_list)
        for an_action in actions:
            out_dict[an_action.target_gae_key.urlsafe()] = True
        false_keys = list(set(target_gae_keys_list)-set(out_dict.keys()))
        for a_key in false_keys:
            out_dict[a_key] = False
    return out_dict

Now I only need the .target_gae_key field, by I cannot project on it because there is an equality filter.

I thought of:

  1. setting target_gae_key as a parent key of the entity, and doing a key query. But it's not pretty, and I also want a similar query on author_gae_key.

  2. in the end, the best would be to do a key only query with an results that would always be the size of the list, with "empty" results when the query doesn't find anything. Does that exist?

Tim Hoffman
  • 12,976
  • 1
  • 17
  • 29
user3217125
  • 579
  • 5
  • 12
  • I would go with 2, however a keys only query will only return the keys that match with no place holders. a get_multi does has a result set along the lines of what you are looking for, however you would need to apply the other filters in memory. – Tim Hoffman Dec 31 '15 at 09:14
  • Thanks @TimHoffman for the tip. However filtering afterwards would probably be too costly in number of reads – user3217125 Dec 31 '15 at 17:27
  • Yeah, becuase you will probably get a few records that don't match your criteria. At the expense of more data stored you could duplicate your `target_gae_key` property with a computed property (saves maintenance) and then get that duplicate via the projection. – Tim Hoffman Jan 01 '16 at 00:46

0 Answers0