让我们说我正在使用此blog post中使用的示例数据。如何使用children()或tree()从所有帖子中获取不同(或没有)标签的列表?
我想我必须要使用孩子,但是我对如何使用它感到困惑。
答案 0 :(得分:0)
这些现在带有示例:children()和tree()。这是一些示例代码,也来自文档:
class KeyData(Model):
key = TextField()
data = JSONField()
KeyData.create(key='a', data={'k1': 'v1', 'x1': {'y1': 'z1'}})
KeyData.create(key='b', data={'x1': {'y1': 'z1', 'y2': 'z2'}})
# We will query the KeyData model for the key and all the
# keys and values in it's data field, recursively.
kd = KeyData.data.tree().alias('tree')
query = (KeyData
.select(kd.c.key, kd.c.value, kd.c.fullkey)
.from_(KeyData, kd)
.order_by(kd.c.key)
.tuples())
print(query[:])
# PRINTS:
[('a', None, '{"k1":"v1","x1":{"y1":"z1"}}', '$'),
('b', None, '{"x1":{"y1":"z1","y2":"z2"}}', '$'),
('a', 'k1', 'v1', '$.k1'),
('a', 'x1', '{"y1":"z1"}', '$.x1'),
('b', 'x1', '{"y1":"z1","y2":"z2"}', '$.x1'),
('a', 'y1', 'z1', '$.x1.y1'),
('b', 'y1', 'z1', '$.x1.y1'),
('b', 'y2', 'z2', '$.x1.y2')]