AppEngine: StringListProperty中的项会始终保持相同顺序吗?

2 投票
2 回答
699 浏览
提问于 2025-04-15 15:18

我查看了App Engine的文档,关于StringListPropertyListProperty,但似乎找不到关于列表中项目顺序是否有保证的信息。也就是说,我想确认在从数据存储中添加和获取数据时,列表的顺序是否会保持不变:

instance = MyModel()
instance.list_property = ['a', 'b', 'c']
instance.put()

# Retrieve the model again
instance2 = MyModel.get_by_id(instance.key().id())
assert instance2.list_property == ['a', 'b', 'c']

App Engine对列表或字符串列表中的项目顺序有任何保证吗?

2 个回答

2

我在文档里没找到明确的说明,不过根据一个关于DataStore的Google I/O的讲座,StringListProperty会按照你放进去的顺序进行排序。

3

是的,根据文档

顺序是被保留的,所以当通过查询和get()方法返回实体时,列表属性的值会和存储时的顺序一样。

这句话出现在我给的链接的第一段最后。

撰写回答