Appengine filter inequality and ordering fails

前端 未结 5 1574
悲哀的现实
悲哀的现实 2021-02-01 18:03

I think I\'m overlooking something simple here, I can\'t imagine this is impossible to do.

I want to filter by a datetime attribute and then order the result by a rankin

5条回答
  •  温柔的废话
    2021-02-01 18:39

    I used another trick, which worked out simply because of the format I needed my data in (a list of dicts). In this case I run the datetime-based query, create dicts from the returned ents, and then sort by the numeric 'counter' property. Reversing the sort gave me a descending order. Keep in mind I only requested 10 results, on a fairly small datastore.

    q = food.Food.all()
    q.filter("last_modified <=", now)
    q.filter("last_modified >=", hour_ago)
    
    ents = q.fetch(10)
    
    if ents:
      results = [{
        "name": ent.name,
        "counter": ent.counter
        } for ent in ents]
    
      # reverse list for 'descending' order
      results.sort(reverse=True)
    

    Example result:

    [{'counter': 111L, 'name': u'wasabi'}, {'counter': 51L, 'name': u'honeydew'}, {'counter': 43L, 'name': u'mars bar'}, {'counter': 37L, 'name': u'scallop'}, {'counter': 33L, 'name': u'turnip'}, {'counter': 29L, 'name': u'cornbread'}, {'counter': 16L, 'name': u'mackerel'}, {'counter': 10L, 'name': u'instant coffee'}, {'counter': 3L, 'name': u'brussel sprouts'}, {'counter': 2L, 'name': u'anchovies'}]
    

提交回复
热议问题