问题
According to Googles Documentation, null values have no sort order. Is there any way to configure this behavior. For instance, if I am sorting an integer property in ascending order, can I configure the datastore to return null values last or first?
回答1:
Your best bet may be to save the property as a blank "" string for each entity that has a null value for that property. Be sure to run put() on each entity that you alter.
employees = Employee.all().fetch(50)
for employee in employees:
if employee.hobbies is None: # Null value property
employee.hobbies = ""
employee.put()
Of course, this is not the most efficient way to perform this task. You may want to create a list object, like "batch_put_list," and append each employee object into the list. Then, perform a db.put(batch_put_list).
batch_put_list = []
employees = Employee.all().fetch(50)
for employee in employees:
if employee.hobbies is None:
employee.hobbies = ""
batch_put_list.append(employee)
db.put(batch_put_list)
Hope this helps.
来源:https://stackoverflow.com/questions/10960094/google-app-engine-null-sort-order