Most of the online tutorials and videos describe how to create and save model instances to the local database using Django. I found this medium article https://medium.com/swlh/create-rest-api-with-django-and-neo4j-database-using-django-nemodel-1290da717df9 explaining how to upload it to Neo4j. I tried to run the same project and this is what I got when I went to http://127.0.0.1:8000/person (see snapshot below).
The "Person" object is a StructuredNode, defined as follows in models/person.py
:
from neomodel import StructuredNode, StringProperty, IntegerProperty,UniqueIdProperty, RelationshipTo
from myapi.models.city import City
class Person(StructuredNode):
uid = UniqueIdProperty()
name = StringProperty(unique_index=False)
age = IntegerProperty(index=True, default=0)
email = StringProperty(unique_index=True)
# Relations :
city = RelationshipTo(City, 'LIVES_IN')
friends = RelationshipTo('Person','FRIEND')
def __unicode__(self):
return self.uid
I want to get the details from a user through a form and save it as an instance of the Person object in my AuraDB: person.save()
So in `views/forms.py' I created a form as such:
from django import forms
from splitjson.widgets import SplitJSONWidget
from myapi.models import Person
class testForm(forms.Form):
attrs = {'class': 'special', 'size': '40'}
data = forms.CharField(widget=SplitJSONWidget(attrs=attrs, debug=True))
class Meta:
model = Person
and I added it as a path to the urls.py
:
from django.urls import re_path as url
from myapi.views import *
from django.urls import path
urlpatterns = [
path('', home, name='home'),
path('home', home, name='home'),
path('sign-up', sign_up, name='sign_up'),
path('test-dict', test_dict, name='test_dict'),
path('person',personDetails),
path('getAllPersons',getAllPersons),
path('city',cityDetails),
path('getAllCities',getAllCities),
path('connectPaC',connectPaC),
path('connectPaP',connectPaP)
]
The path with the name test-dict
leads to this function defined in home.py
, which tries to access the testForm
created earlier.:
from django.template import RequestContext
from .forms import testForm
from django.template.loader import render_to_string
def test_dict(request):
json = {'name': 1,
'age': 4}
form = testForm(request.POST or None, initial={'data': json})
if form.is_valid():
# validate and save
pass
template = 'myapi/test_template.html'
#context = RequestContext(request, render_to_string(form))
return render(request, template, {'form': form})
And finally, this is how the views/persons.py
looks like:
from django.http import JsonResponse
from myapi.models import Person
from django.views.decorators.csrf import csrf_exempt
import json
from django.shortcuts import render, redirect
def getAllPersons(request):
if request.method == 'GET':
try:
persons = Person.nodes.all()
response = []
for person in persons :
obj = {
"uid": person.uid,
"name": person.name,
"age": person.age,
}
response.append(obj)
return JsonResponse(response, safe=False)
except:
response = {"error": "Error occurred"}
return JsonResponse(response, safe=False)
@csrf_exempt
def personDetails(request):
if request.method == 'GET':
# get one person by name
name = request.GET.get('name', ' ')
try:
person = Person.nodes.get(name=name)
response = {
"uid": person.uid,
"name": person.name,
"age": person.age,
}
return JsonResponse(response, safe=False)
except :
response = {"error":"Error occurred"}
return JsonResponse(response, safe=False)
#return render(response, 'myapi/test_template.html')
if request.method == 'POST':
# create one person
json_data = json.loads(request.body)
name = json_data['first_name']
email = str(json_data['email'])
try:
person = Person(name=name, email=email)
person.save()
response = {
"uid": person.uid,
}
return JsonResponse(response)
except :
response = {"error":"Error occurred"}
return JsonResponse(response, safe=False)
if request.method == 'PUT':
# update one person
json_data = json.loads(request.body)
name = json_data['name']
age = int(json_data['age'])
uid = json_data['uid']
try:
person = Person.nodes.get(uid=uid)
person.name = name
person.age = age
person.save()
response = {
"uid": person.uid,
"name": person.name,
"age": person.age,
}
return JsonResponse(response, safe=False)
except:
response = {"error":"Error occurred"}
return JsonResponse(response, safe=False)
if request.method == 'DELETE':
# delete one person
json_data = json.loads(request.body)
uid = json_data['uid']
try:
person = Person.nodes.get(uid=uid)
person.delete()
response = {"success": "Person deleted"}
return JsonResponse(response, safe=False)
except:
response = {"error":"Error occurred"}
return JsonResponse(response, safe=False)
But it is still not working. I want to know how is it possible to create an instance of a 'Person' based on the input from a user through a form, and save it to AuraDB (the connection to AuraDB is working fine, by the way). I guess it is surely possible, but since I am absolutely new, I might not be implementing it in the right way.