在 Python/Google 应用引擎中,我想将属性存储为键名,以节省资源并加快速度。但是我不知道如何获取键名列表。
例如,考虑数据模型:
class Book(db.Model):
isbn = db.StringProperty() # Make this a key name instead.
category = db.StringProperty()
author = db.StringProperty()
title = db.StringProperty()
...
现在假设我将键名称设置为 isbn,而不是将其设为属性名称:
new_book = Book(category="fantasy,comedy", title="The Light Fantastic", author="Terry Pratchett", key_name=isbn_number)
我想,如果我能找到一种获取键名列表的方法,我会节省资源吗?例如,在检查某本书是否已在馆藏中时。
这是否正确?如何获得键名列表?
如果不是这种情况,那么以下是一个不错的折衷方案:
class ISBN(db.Model):
isbn = db.StringProperty()
class Book(db.Model):
isbn = db.ReferenceProperty(ISBN)
category = db.StringProperty()
author = db.StringProperty()
title = db.StringProperty()
...
它会节省资源/更快吗?
最佳答案
对于第一种方法,ISBN 作为键:
class Book(db.Model):
#isbn is key, so it shouldn't be a property
category = db.StringProperty()
author = db.StringProperty()
title = db.StringProperty()
new_book = Book(category="fantasy,comedy", title="The Light Fantastic", author="TerryPratchett", key_name=isbn_number)
然后获取所有 ISBN 的列表:
isbns = [b.name() for Book.all(keys_only=True)]
关于python - 如何获取 Python/Google 应用引擎中的键名列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5407647/