django model.objects.all()do no not()给予所有对象
在我的Django型号中,我有2个字段。当我在下面执行代码时,它只会打印分辨率字段。如何将所有字段数据获取在列表中?
x = ResolutionsModel.objects.all()
for i in x:
print(i)
型号
class ResolutionsModel(models.Model):
resolution = models.TextField(max_length=30,blank=True)
act_abbreviation = models.TextField(max_length=30)
def __str__(self):
return self.resolution
In my Django model I have 2 field. When I execute below code it just prints the resolution field. How can I get the all fields data in a list?
x = ResolutionsModel.objects.all()
for i in x:
print(i)
models.py
class ResolutionsModel(models.Model):
resolution = models.TextField(max_length=30,blank=True)
act_abbreviation = models.TextField(max_length=30)
def __str__(self):
return self.resolution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,您的模型说,要将自己作为字符串表示为字符串,则应使用
resolution
的值。因此,通过打印一个实例,这就是您所获得的 -resolution
的值。如果将QuerySet传递到模板,则可以从所有字段中输出值。
为了在Python中进行测试,您必须具体包含每个字段;
如果您实际上想在列表中获取数据,则可能需要阅读有关在QuerySet上使用
values_list
的信息; https://docs.djangoproject.com/44.0/4.0 /ref/models/querysets/#values-list为了了解Django,您可以也适应您的str方法;
So your model says that to represent an instance of itself as a string it should use the value of
resolution
. So by printing an instance, that's what you're getting - the value ofresolution
.If you pass your queryset to a template you could output the values from all the fields.
For the purposes of your test in python you'd have to specifically include each field;
If you actually want to get data in a list, you might want to read about how to use
values_list
on a queryset; https://docs.djangoproject.com/en/4.0/ref/models/querysets/#values-listFor the purpose of getting to know django you could also adapt your str method;
在您的情况下:
x此处是一个查询集,它返回数据库中的一堆条目,每个条目都是数据库条目:
最后,一切都是对象。
In your case:
The x here is a query set, which returns a bunch of entries from data base, each entry is a database entry:
In the end, everything is an object.