0

我正在使用 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 应用程序获得相同的输出,但我得到的是:

Sargans orig loc000929
Sargans std loc000929
Sargans orig loc000929
Sargans orig loc000929
Sargans std loc000929
Sargans orig loc000929
Sargans orig loc000929
Sargans orig loc000929 

好像第二个循环每次都经过同一个实体......

地名类:

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 %}

如何编写正确的模板?

4

1 回答 1

1

您需要每个 placename的完整结果列表。

在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 %}
于 2014-07-01T15:26:08.020 回答