Search code examples
pythonpy2neo

MVC and OOP in py2neo


I'm looking for some "best practice" coding style using py2neo. Coming from Django ORM and Bulbs (another python neo4j library), I'm used to MVC-style separation of model from controller like this:

class Node_Type_A(Node):
    element_type = 'A node'
    modified = DateTime(default=now())

    def custom_A_method(self):
        pass

I read that this is done with metaclasses, I think it's not available in py2neo and it may well be overkill.

But I'm wondering about a good pragmatic py2neo coding style to achieve:

  • some node/relationship templating (does node type A have a modified var? Of what type? Of what type is my node node_b?)
  • keeping variables and methods together per node type:

    res = graph_db.create({dict})
    res.custom_A_method()
    
  • saving changed variables back into the database such as a.modified = now()

What's the way to do it in py2neo? Thanks for any hints!


Solution

  • If you're working with (or coming from) Django then I recommend you look at neomodel by Rob Edwards. It's built on top of py2neo especially for use within Django but is equally usable outside that environment. It's designed for a Django model-esque coding style which should be familiar and hopefully give you what you're looking for!