【发布时间】:2018-03-11 11:11:02
【问题描述】:
我事先道歉,因为 Django 的思维方式对我来说仍然很陌生。我正在尝试生成一个非常简单的页面,该页面仅列出使用 Neo4j 和 Django(1.9.7)的简单密码查询的所有结果,并且我正在使用 Python Neo4j 驱动程序从 Django 访问数据库。但是,我陷入困境并且已经达到了我只是盲目地尝试事物的地步,因此我想要一些关于我试图实现的基础应该如何看待的指针/建议。
models.py
from django.views.generic.listimport ListView
from neo4j.v1 import GraphDatabase, basic_auth
from django.db import models
# Connect to DB
driver=GraphDatabase.driver("foo1",auth=basic_auth("foo2","foo3"))
session=driver.session()
class Stuff(models.Model):
query = "MATCH (t:Time) return t"
results=session.run(query)
# Sanity check -> This just shows that the database and query both work
for foo in results:
print foo
break
def __str__(self):
return results
views.py
from django.views.generic.list import ListView
from .models import Stuff
# I assume that I should be using a ListView here (as I was trying to get a queryset or similar from my models).
class IndexView(ListView):
template_name = 'index.html'
def get_queryset(self):
fooList = []
for record in Stuff.objects.get():
fooList.append(record)
return fooList
index.html(未测试,因为我还没有设法让它“显示”)
{% block body %}
{% if fooList %}
<h1>Woot!</h1>
{% endif %}
{% endblock %}
上述位显然不起作用并抱怨Stuff 没有任何objects,但我完全不知道如何继续(因为我找不到任何关于使用此驱动程序的好的示例/文档在 Django 中)。
【问题讨论】:
-
你是说你没有从 Neo4j 查询中得到
results吗?您能否更具体地说明您面临的问题。