Django查询集选择多列并在模板中获取实例对象

[英]Django query set select multiple columns and get instance object in template


I have the following code to get selected columns in Django

我有以下代码来获取Django中的选定列

>>> l = Listing.objects.values("id", "City")[:10]
>>> l
[{'City': u'GLENN DALE', 'id': 1459}, {'City': u'SHADY SIDE', 'id': 1460}, {'City': u'BALTIMORE', 'id': 1474}, {'City': u'BALTIMORE', 'id': 1463}, {'City': u'EDGEWOOD', 'id': 1464}, {'City': u'CAPITOL HEIGHTS', 'id': 1466}, {'City': u'ROCK HALL', 'id': 1465}, {'City': u'RIVA', 'id': 1468}, {'City': u'PRINCE FREDERICK', 'id': 1469}, {'City': u'FREDERICK', 'id': 1470}]
>>> 

If I pass the "l" object to the template, I will have a list of dictionary. However, I have a function that's attached to the Listing model, and need it call it from the template.

如果我将“l”对象传递给模板,我将有一个字典列表。但是,我有一个附加到Listing模型的函数,需要它从模板中调用它。

The following will output nothing, how do I get back to the object?

以下将不输出任何内容,如何返回对象?

{% for obj in l %}
   {{obj.get_images}}
{% endfor %}

1 个解决方案

#1


1  

Try using only instead of values like:

请尝试仅使用以下值:

l = Listing.objects.only("id", "City")[:10]

Get more info about only here

仅在此处获取更多信息


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/11/09/2b055efa5b290d6224e86f110cd14e26.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号