如果通过获取查询集中的第一个对象属性来访问,Django 对象的属性将变为字符串

发布于 2025-01-17 18:17:06 字数 532 浏览 2 评论 0原文

我有一个问题,如果我访问像 queryset.0.attr1 这样的查询集中的对象属性,它会丢失它的类型并变成一个字符串(Django 模板处理器像字符串一样处理它)

简单的例子:我有一个查询集,它有相关的集合,但是当我通过 index.attribute_name 访问此相关集第一个对象的属性时,它变成字符串。如果我在循环中执行相同的操作 - 该属性保持其类型,

{% for p in periods %}
BECOMES STRINGS accessed via .0.attr
{{ p.productions.0.time }} str
{{ p.productions.0.amount }} str
MANTAIN TYPES accessed in a loop
{% for pp in p.productions %}
{{ pp.amount }} timedelta
{{ pp.time }} decimal
{% endfor %}{% endfor %}

我希望以某种方式使用其原始类型访问查询集中第一个对象的属性

I have a problem that if I access objects attributes in queryset like queryset.0.attr1 it looses it’s type and become a string (Django template processor processes it like a string)

Simple example: I got a queryset, that have related set, but when I access this related set first object's attribute by index.attribute_name it becomes string. If I do the same thing in a loop - this attribute maintain its type

{% for p in periods %}
BECOMES STRINGS accessed via .0.attr
{{ p.productions.0.time }} str
{{ p.productions.0.amount }} str
MANTAIN TYPES accessed in a loop
{% for pp in p.productions %}
{{ pp.amount }} timedelta
{{ pp.time }} decimal
{% endfor %}{% endfor %}

I want somehow to access attributes of first object in queryset with it’s original type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2025-01-24 18:17:06

我应该重新启动服务,现在一切都很好

I should just restart service, everything is fine now

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文