0

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!

bebbi
  • 2,489
  • 3
  • 22
  • 36

1 Answers1

2

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!

Nigel Small
  • 4,475
  • 1
  • 17
  • 15
  • Great, this is exactly what I was looking for! But being a newbie with this, I'm very curious to know whether this additional model layer is just overhead for the seasoned programmer. So, how would you deal with associating methods with neo4j nodes when coding with py2neo yourself? Would you write plain functions (MVC??), or use neomodel yourself or somehow build your own association classes? I'm not using django anymore and would like to keep complexity as low as possible while still doing MVC. – bebbi Apr 02 '13 at 12:43
  • 1
    It's not overhead inasmuch as it provides a layer of abstraction which py2neo itself doesn't. The API provided by py2neo is closer to that exposed by Neo4j whereas the neomodel API is closer to Django. Are you writing a web application? If so, are you using a (non-Django) framework? If not, you might want not want to focus too heavily on an MVC architecture as that is most typically used in a web context. – Nigel Small Apr 02 '13 at 16:39
  • Flask is an excellent choice. Neomodel should work fine for your model layer. – Nigel Small Apr 03 '13 at 12:48
  • 1
    2-years later, thanks for mention of neomodel. It's exactly what I needed to abstract up a level to get my data model from Flask implemented in Neo4j. Bulbs looked promising, but the repo was dead to the world. Life is good! – horcle_buzz Nov 16 '15 at 03:17