从Django模板中的模型对象列表中获取特定属性的列表

时间:2021-07-28 20:19:31

Is there a way to get a list of a specific attribute from a list of model objects, {{ object_list }} using the Django Template Language?

有没有办法使用Django模板语言从模型对象列表{{object_list}}获取特定属性的列表?

Similar to this in Python? [o.my_attr for o in object_list]

在Python中与此类似? [o.my_attr for object in object_list]

1 个解决方案

#1


1  

You can't really do list comprehensions inside Django templates. You should do this in your view and pass the list in your context to the template.

你不能真正在Django模板中做列表推导。您应该在视图中执行此操作,并将上下文中的列表传递给模板。

#1


1  

You can't really do list comprehensions inside Django templates. You should do this in your view and pass the list in your context to the template.

你不能真正在Django模板中做列表推导。您应该在视图中执行此操作,并将上下文中的列表传递给模板。