By using a custom authentication backend, you can make use of Django’s authentication framework while storing users in Neo4j.

Add neo4django.auth in your INSTALLED_APPS setting, and add:

AUTHENTICATION_BACKENDS = ('neo4django.auth.backends.NodeModelBackend',)

in your

To create a new user, use something like:

user = User.objects.create_user('john', '', 'johnpassword')

Login, reset password, and other included auth views should work as expected. In your views, user will contain an instance of neo4django.auth.models.User for authenticated users.

Referencing Users

Other models are free to reference users. Consider:

from django.contrib.auth import authenticate

from neo4django.db import models
from neo4django.auth import User

class Post(models.NodeModel):
    title = models.StringProperty()
    author = models.Relationship(User, rel_type='written_by', single=True,

user = authenticate(username='john', password='johnpassword')

post = Post()
post.title = 'Cool Music Post' = user

assert list(user.posts.all())[0] == post

Customizing Users

Swappable user models are in the works, but until then users can be customized by subclassing:

from neo4django.db import models
from neo4django.auth import User

class TwitterUser(User):
    follows = models.Relationship('self', rel_type='follows',

jack = TwitterUser()
jack.username = 'jack' = ''

jim = TwitterUser()
jim.username = 'jim' = ''

The caveats are, first, that User manager shortcuts, like create_user(), aren’t available, and that authenticate() and other included functions to work with users will return the wrong model type. This is fairly straightforward to handle, though, using the included convenience method from_model():

from django.contrib.auth import authenticate

user = authenticate(username='jim', password='jimpassword')
twitter_user = TwitterUser.from_model(user)


Because neo4django doesn’t support django.contrib.contenttypes or an equivalent, user permissions are not supported. Object-specific or contenttypes-style permissions would be a great place to contribute.