9

I apologize before hand as the Django way of thinking is still very alien to me. I am trying to generate a very simple page that justs lists all results from a simple cypher query using Neo4j and Django (1.9.7) and I am using the Python Neo4j driver to access the database from Django. However, I am getting stuck and have reached the point where I am just blindly trying things, as such I would like some pointers/advice on how the basics of what I am trying to achieve should look.

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 (not tested as I haven't managed to get this to 'show' yet)

{% block body %}

{% if fooList %}
  <h1>Woot!</h1>
{% endif %}

{% endblock %}

The above bits obviously don't work and complain about Stuff not having any objects, yet I am totally lost on how to continue (as I have been unable to find any good examples/documentation on using this driver inside Django).

Bas Jansen
  • 3,273
  • 5
  • 30
  • 66
  • Are you saying that you are not getting `results` from Neo4j query? Could you please be more specific with the issue you are facing. – Anwar Shaikh Oct 10 '17 at 17:09

2 Answers2

1

Documentation of session object in neo4j python driver run method state that

run(statement, parameters=None, **kwparameters)

it returns StatementResult object as documented here

So according to the docs there is no objects property and therefore .objects.get() method does not exists.

Right way to do access records in returned StatementResult is shown in example as following:

for record in result:
      print("%s %s" % (record["title"], record["name"]))

So in your case you may want to do:

for record in Stuff:
  fooList.append(record)
hlihovac
  • 368
  • 2
  • 10
  • That is one of the things that I had tried previously, which yields `'ModelBase' object is not iterable`. – Bas Jansen Oct 01 '17 at 14:08
  • Acording this SO [answer](https://stackoverflow.com/questions/18512491/typeerror-object-is-not-iterable) You can't iterate over a model instance. However there is solution if you still require to to it. – hlihovac Oct 01 '17 at 21:02
  • I am still loooking for a way to get this working (being able to query a neo4j database and showing the results using this driver), just be aware that while you are focussing on the `model` that I myself am not 100% sure that using `model` in such a way is even correct when using an existing neo4j database. – Bas Jansen Oct 05 '17 at 07:49
  • Google suggests following links you may find useful: - [neo4j-django-tutorial](https://github.com/johanlundberg/neo4j-django-tutorial) - [neo4django](https://github.com/scholrly/neo4django) – hlihovac Oct 06 '17 at 14:58
0

you can write a flat REsTFul API to communicate with a frontend maybe written in React Angular2 to dump and display your data. So first , you can use DRF(Django rest Framework), then everything would happen mostly in your views.py and serializers.py and a bit in your models.py. Why avoiding Django template, the query load might affect your app to run smoothly.

Patcho
  • 77
  • 8