以前使用walrus大部分情況用的都是TextField類型,今天使用ListField時遇到一些坎坷,所以把今天的問題在這里記錄一下裂逐。
使用TextField時直接可以用 cls.create()? 給redis設(shè)置值,但是ListField時會報錯:
ValueError: Cannot set the value of a container field.
意思是不可以插入值在容器類里邊(ListField是一個container field)
在網(wǎng)上搜了好久也沒找到答案,然后就在官方文檔里邊找到這樣一句話:
就是說在ListField中插入值必須先實例化一個對象议双,利用對象的方法進行插值,文檔中也給出了這樣一個案例:
class Note(Model):
? ? __database__ = db
? ? text = TextField()
? ? timestamp = DateTimeField(
? ? ? ? default=datetime.datetime.now,
? ? ? ? index=True)
? ? tags = SetField()
>>> note = Note.create(content='my first note')
>>> note.tags
<Set "note:container.tags.note:id.3": 0 items>
>>> note.tags.add('testing', 'walrus')
>>> Note.load(note._id).tags
<Set "note:container.tags.note:id.3": 0 items>
ok捉片!問題解決平痰!