2

我将如何根据属性的实体获取数据存储实体?我是否必须经历一个多步骤的 filter() get() 过程?或者我可以通过 1 件 get() 完成此操作吗?

例如,假设我有 Datastore 模型 Channel:

class Channel(db.Model):
    name = db.StringProperty(required = True)

现在,如果我想根据它的名称删除一个频道,我该怎么做:

name = "ch1"
channel = Channel.get("name = " name)
channel.delete()

这个 get 语句不起作用。实现这一目标的实际方法是什么?非常感谢!

4

3 回答 3

1

If you're doing this, you're better off changing your design slightly. If you're fetching channels by name, then it would be far easier (and faster and cheaper) to use the name of the channel as the key name instead.

Just pass the key_name parameter when creating the entity (examples borrowed from the docs):

employee = Employee(key_name='asalieri',
                first_name='Antonio',
                last_name='Salieri')

And fetch it by with

k = Key.from_path('Employee', 'asalieri')

Once you have the key, all other operations can proceed as usual.

于 2013-04-17T16:28:28.337 回答
1

这是根据属性检索实体的方法

q = db.Query(Channel)
q.filter("name =", name)
entities = q.fetch(100) # The number of entities you want to be returned
if entries:    
    db.delete(entries)
于 2013-04-17T16:08:22.137 回答
0

如果名称字段是唯一标识一个实体,最好将其用作 Sudhir 提到的键。但是,如果有多个实体具有相同的名称,您需要先获取它们

all = Channel.all(keys_only=True).fetch(1000) # only need key to delete
db.delete(all)

一行版本

db.delete(Channel.all(keys_only=True).fetch(1000))
于 2013-04-17T17:45:19.077 回答