GAE Voice Modeling

I am trying to determine the most efficient way to create a voted object in a GAE datastore. I would like to show the user a voting control for this person or an icon indicating that they have already voted for him; those. I ask: "User voted for this organization?" Suppose we have a Question object that the user can vote on. Here is what I am going to do:

  • Request for Question objects. These questions already have a pre-calculated rating by which I will sort.
  • Use a relation index object that is a child of the Question object. A query for all Questions using the same filters as # 1, where my user is a member of this relationship index object.
  • Combine results # 2 into # 1 by setting the foundVoted property to true for each item in the set.

This is the cleanest way I could think of, but it still requires two queries. I did not create duplicate Question objects for each user because this would cause too much data duplication. Is this a good way to deal with what is actually the mix between the m2m relationship between Voices and Questions, or am I thinking too relationally?

+3
source share
3 answers

, . _ . , , y x, (: x/Vote: y). .

+3

Overheard Google App Engine.

, , , .

Google .

+2

, , . User " " .

( memcache, ), , ( ).

, , , . ( ) , :

class UserVotes(db.Model):
    # key = key_name or ID of the corresponding user entity

    # if all of your question entities have IDs, then voted_on can be a list of 
    # integers; otherwise it can be a list of strings (key_name values)
    voted_on = db.ListProperty(int, indexed=False)  

# in your request handler ...
questions = ...
voted_on = memcache.get('voted-on:%s' % user_id)
if voted_on is None:
    voted_on = UserVotes.get_by_id(user_id)  # should do get_or_insert() instead
    memcache.set(...)
for q in questions:
    q.has_voted = q.key().id() in voted_on
0

Source: https://habr.com/ru/post/1776303/


All Articles