我正在使用Django我的NoSQL图形数据库,以及用于链接它们的Neomodel绑定。在我的本地shell中查询它,我有一切正确:
city = PlaceName.index.search(name='Sargans')
for x in city:
results = x.traverse('hasid')
for y in results:
print x.name, x.type, y.loc_id
...:
Sargans orig loc000928
Sargans std loc000010
Sargans orig loc000010
Sargans orig loc000654
Sargans std loc000925
Sargans orig loc000063
Sargans orig loc000011
Sargans orig loc000929
我希望通过我的Django-app获得相同的输出,但我得到的是:
Sargans orig loc000929
Sargans std loc000929
Sargans orig loc000929
Sargans orig loc000929
Sargans std loc000929
Sargans orig loc000929
Sargans orig loc000929
Sargans orig loc000929
好像每次第二个循环遍历同一个实体......
PlaceName类:
class PlaceName(StructuredNode):
name = StringProperty(index=True, required=True)
lang = StringProperty(index=True, required=True)
type = StringProperty(index=True, required=True)#std/orig
hasid = RelationshipTo('PlaceId', 'HAS_ID')
descr = RelationshipTo('Desc', 'HAS_DESC')
在我的views.py中:
placenames = PlaceName.index.search(name=q)
for x in placenames:
results = x.traverse('hasid')
return render_to_response('search_results.html',
{'placenames':placenames, 'query':q, 'res':results})
在我的模板中:
{% for x in placenames %}
{% for y in res %}
<a> {{ x.name }} {{ x.type }} {{ y.loc_id }} </a><br></li>
% endfor %}
{% endfor %}
如何编写正确的模板?
答案 0 :(得分:1)
您需要每个地名的结果的完整列表。
在views.py中:
places = [{'placename': x, 'results': x.traverse('hasid')} for x in placenames]
return render_to_response('search_results.html', {'places': places})
在模板中:
{% for x in places %}
{% for y in x.results %}
<a> {{ x.placename.name }} {{ x.placename.type }} {{ y.loc_id }} </a><br></li>
{% endfor %}
{% endfor %}