【发布时间】:2021-07-27 10:21:07
【问题描述】:
我对 django 有疑问。在我的模型中,我有一个 JSON 字段,它默认存储一个空列表。当尝试将值附加到该列表时,这不起作用。无论它是另一个对象还是字符串。去田野,那时还是空的。我将不胜感激任何帮助。提前致谢。
models.py
followers = models.JSONField(default={
"followers": []
})
(“关注者”字段在客户端类中)
views.py
current_user = Client.objects.get(id=response.user.id)
current_user.followers["followers"].append("Some Name")
【问题讨论】:
-
你调用
current_user.save()方法了吗? -
您不应该使用字典 object 作为默认值,因为这样所有新构造的项目都将引用 same 字典。
-
天哪,那是错误。对不起哈哈这很容易xD
标签: python django django-models