1

Google App Engine 用に Danny Hermes によって作成された Endpoints-proto-datastore を使用しており、エンティティを更新する方法を理解するのに助けが必要です。更新する必要があるもののモデルは次のとおりです。

class Topic(EndpointsModel):
    #_message_fields_schema = ('id','topic_name','topic_author')
    topic_name = ndb.StringProperty(required=True)
    topic_date = ndb.DateTimeProperty(auto_now_add=True)
    topic_author = ndb.KeyProperty(required=True)
    topic_num_views = ndb.IntegerProperty(default=0)
    topic_num_replies = ndb.IntegerProperty(default=0)
    topic_flagged = ndb.BooleanProperty(default=False)
    topic_followers = ndb.KeyProperty(repeated=True)
    topic_avg_rating = ndb.FloatProperty(default=0.0)
    topic_total_rating = ndb.FloatProperty(default=0.0)
    topic_num_ratings = ndb.IntegerProperty(default=0)
    topic_raters = ndb.KeyProperty(repeated=True)

ご覧のとおり、評価プロパティの既定値は 0 です。そのため、トピックが評価されるたびに、各評価プロパティを更新する必要があります。ただし、私のプロパティはどれも、ユーザーによって提供された実際の評価ではありません. ユーザーがトピックを評価した値を渡して、モデルのプロパティを更新できるようにするにはどうすればよいですか? ありがとう!

4

1 に答える 1

1

これを行うには、「エイリアス」プロパティを次のようratingに関連付けますUserModel

from endpoints_proto_datastore.ndb import EndpointsAliasProperty

class UserModel(EndpointsModel):

    ...

    def rating_set(self, value):
        # Do some validation
        self._rating = value

    @EndpointsAliasProperty(setter=rating_set)
    def rating(self):
        return self._rating

これにより、リクエストに s を付けて評価を送信できUserModelますが、これらの評価を保存する必要はありません。

ユーザーに OAuth 2.0 トークンを使用endpoints.get_current_user()し、要求に含まれるユーザーを特定するために呼び出すことをお勧めします。

評価専用のモデルのようなものは、はるかに簡単になる可能性があります。

from endpoints_proto_datastore.ndb import EndpointsUserProperty

class Rating(EndpointsModel):
    rater = EndpointsUserProperty(raise_unauthorized=True)
    rating = ndb.IntegerProperty()
    topic = ndb.KeyProperty(kind=Topic)

次に、データストアから をトランザクションで取得し、Topicで装飾されたリクエスト メソッドで更新します@Rating.method

于 2013-07-09T21:06:13.870 に答える