9

I'm build a Django app with Neo4j (along with Postgres), I found this Django integration called neo4django, I was wondering if it's possible to use neo4restclient only, like, what would be the disadvantages of not using Neo4django? Does using neo4-rest-client only, give me more flexibility? When I was creating my models with Neo4Django, it seemed that there is no difference between modeling a graph db and relational db. Am I missing anything?

Thanks!

Matt Luongo
  • 14,371
  • 6
  • 53
  • 64

1 Answers1

12

You can absolutely go ahead with neo4j-rest-client or py2neo, without using neo4django. In the same way, you can use any other database driver you'd like any time using Django, any REST client, etc.

What'll you lose? The model DSL, the built-in querying (eg, Person.objects.filter(name="Mohamed")), the built-in indexing, and the Lucene, Gremlin and Cypher behind that. Some things will be much easier- like setting an arbitrary property on a node- but you'll need to learn more about how Neo4j works.

You'll also lose some of the shortcuts Django provides that work with neo4django, like get_object_or_404() and some of the class-based views that work with querysets.

What'll you gain? Absolute power over the DB, and an easier time tweaking DB performance. Though neo4django isn't nearly as good a lib as some traditional ORMs in the Python sphere, the trade-off of power vs provided ease is similar.

That said, the two can work together- you can drop down from neo4django to the underlying REST client nodes and relationships anytime. Just use model_instance.node to get the underlying neo4j-rest-client node object from a model, and from neo4django.db import connection to get a wrapped neo4j-rest-client GraphDatabase.

On whether you're missing something: neo4django was written to re-use a powerful developer interface- the Django ORM- so it should feel similar to writing models for Postgres. I've written a bit about that odd feeling in the past. I think part of the problem might be that the lib doesn't highlight the graph terminology new graph-interested devs expect- like traversals and pattern matching- and instead dresses those techniques in Django query clothing.

I'd love your thoughts, or to know anything you'd like the library to do that it isn't doing :) Good luck!

Matt Luongo
  • 14,371
  • 6
  • 53
  • 64
  • 1
    Thanks a lot for your elaborate and clear response Matt ! It wasn't clear to me that I could use both the neo4j-rest-client and neo4django at the same time with model_instance.node. This sounds great. One thing I noticed was that querying is a bit slow tho. Also, I'd love to use authentication against Neo4j only instead of using Postgres (or another relational db), so maybe I'll build that myself as a separate reusable Django app. You think it' worth it? I mean, I would love to use `userena` but then I'll have three models for one user (Django's model, Userena's model and the neo4django's) –  Feb 15 '13 at 08:31
  • 1
    Hm, what sort of queries are too slow for you? There's a good chance I can do something about it :) I'm working on a solution for auth in Neo4j as well. What I'd really like is a drop-in replacement for `contrib.auth` that works with Django 1.5's swappable user models and keeps everything in the graph. I'm still not certain when I'll have that ready, though. – Matt Luongo Feb 16 '13 at 14:50
  • Something as simple as `MyModel.objects.filter(username="mo")` is slower than what I'm used to. About the auth, that's exactly what i'm aiming at, replacing the `contrib.auth`. I'm now using `django-userena` to handle all auth, login, profile editing adn everything but I'm sure it's a messy solution. Any chance I can contribute to the auth app as I was planning to build the solution anyway? ;) –  Feb 16 '13 at 15:26
  • Please! In fact, if you're up to it put it up on GitHub and I'll happily contribute, or we can both do our own thing, and compare approaches. I have a feeling you'll be on it before I will, though:) Re: filter queries - have you set an index on the username field? – Matt Luongo Feb 16 '13 at 20:26
  • I'm starting to think a custom auth backend, similar to mongoengine's, + a contribution to social_auth for neo4django-based models would be the way to go. – Matt Luongo Feb 24 '13 at 22:39
  • I've started work on this. The initial efforts are in the "auth" branch- I plan to add social_auth support as well. No docs yet, but if you'd like to try it check out the branch and add "neo4django.auth.backends.NodeModelBackend" to AUTHENTICATION_BACKENDS in settings.py. – Matt Luongo Feb 25 '13 at 05:38
  • Great ! I'm trying to finish the first iteration of my project and then focus on this. I'll give it a try for sure, just checked the source code tho. I'll get back to you ;) –  Feb 25 '13 at 08:09
  • nice explanation @matt-luongo ! is there any update on your auth-backend? – Devang Hingu Mar 22 '23 at 12:44