[英]Accessing model data in django
I have aa Django model:我有一个 Django model:
class Myvalues(models.Model):
items_list = models.JSONField()
I have populated this model in Django admin.我在 Django 管理员中填充了这个 model。 I now want to access the data inside this model.
我现在想访问这个 model 里面的数据。 However I am getting an error that says:
'Manager' object has no attribute 'items_list'
The way I am trying to access it is as follows:但是我收到一条错误消息:
'Manager' object has no attribute 'items_list'
我尝试访问它的方式如下:
Print(Myvalues.objects.items_list.values())
I don't understand why this is coming up as the class has that model in it.我不明白为什么会出现这种情况,因为 class 里面有 model。 Out of interest I printed the
all()
result of Myvalues
class, like so:出于兴趣,我打印了
Myvalues
class 的all()
结果,如下所示:
print(Myvalues.objects.all())
this resulted in the following:结果如下:
<QuerySet [<Myvalues: Myvalues object (1)>]>
How can I access the data that's in my items_list model?如何访问我的 items_list model 中的数据?
Use the following syntax instead ( documentation ).请改用以下语法( 文档)。
Myvalues.objects.values_list('items_list', flat=True)
If you want to have tuples returned, omit flat=True
.如果要返回元组,请省略
flat=True
。
Myvalues.objects
returns a django.db.models.manager.Manager
object, not a Myvalues: Myvalues object
返回
django.db.models.manager.Manager
object,而不是Myvalues: Myvalues object
In order to request the items_list
use为了请求
items_list
使用
Myvalues.objects.all()[i].item_list
It returns all the Myvalue
objects and picks one with the index.它返回所有
Myvalue
对象并选择一个带有索引的对象。 From this object you can get your model properties.从此 object 您可以获得 model 属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.