google-app-engineendpoints-proto-datastore

Spit max entity from query


How do I get max model given parameter from query?

What i want is get xs parameter, compare xs max in ndb.

If max_ndb(xs) is higher return the max_model, otherwise return 204

   @MyModel.query_method(requests_fields('xs',),
                               name='infofest',
                               path='mymodel',
                               http_method='GET'
                               )
    def get_model(self,query):
        return max(query, key=MyModel.xs)#??

Of course query itself is not iterable,and i may not use query_method instead, Something like.

@MyModel.method(request_fields=('xs',),
                  path='mymodel/{xs}', http_method='GET', name='mymodel.get')   def MyModelGet(self, my_model):
     if not my_model.maxXS_from_datastore:
        return model_with_maxXS
     return 204

Or can we use GQL in endpoints-proto-datastore? Thanks


Solution

  • If what you mean is "grabbing the max xs from datastore", it is not doable directly, but it can be easily achieved by using a sort on your query, and taking the first result.

    Something like (this is more/less pseudo code, I never used python's ndb, I usually work with java's objectify, but the logic is the same) :

    myModel.query().sort("xs descending");

    At this point, the first result of that query will simply be your highest "xs" object. You can do your comparison on that first object.