4. py2neo.ogm – Object-Graph Mapping

The py2neo.ogm package contains a set of facilities for binding Python objects to an underlying set of graph data. Class definitions extend GraphObject and include Property and Label definitions as well as details of Related objects.

A simple example, based on the movie graph data set:

class Movie(GraphObject):
    __primarykey__ = "title"

    title = Property()
    tag_line = Property("tagline")
    released = Property()

    actors = RelatedFrom("Person", "ACTED_IN")
    directors = RelatedFrom("Person", "DIRECTED")
    producers = RelatedFrom("Person", "PRODUCED")

class Person(GraphObject):
    __primarykey__ = "name"

    name = Property()
    born = Property()

    acted_in = RelatedTo(Movie)
    directed = RelatedTo(Movie)
    produced = RelatedTo(Movie)

4.1. Graph Objects

At the heart of the py2neo OGM framework is the GraphObject. This is a base class for all classes that are to be mapped onto the graph database. Each GraphObject wraps a node as well as a set of pointers to RelatedObjects and the relationship details that connect them.

A GraphObject instance may be constructed just like any other Python object but can also be matched from the database. Each instance may contain attributes that represent labels, nodes or related objects.

class py2neo.ogm.GraphObject

The primary node label used for Cypher MATCH and MERGE operations. By default the name of the class is used.


The primary property key used for Cypher MATCH and MERGE operations. If undefined, the special value of "__id__" is used to hinge uniqueness on the internal node ID instead of a property. Note that this alters the behaviour of operations such as Graph.create() and Graph.merge() on GraphObject instances.


The value of the property identified by __primarykey__. If the key is "__id__" then this value returns the internal node ID.


The Node wrapped by this GraphObject.

classmethod GraphObject.wrap(node)

Convert a Node into a GraphObject.




classmethod GraphObject.match(graph, primary_value=None)

Select one or more nodes from the database, wrapped as instances of this class.

  • graph – the Graph instance in which to match

  • primary_value – value of the primary property (optional)

Return type


4.2. Properties

A Property defined on a GraphObject provides an accessor to a property of the underlying node.

class py2neo.ogm.Property(key=None, default=None)[source]

Property definition for a GraphObject.


The name of the node property within the database.


The default value for the property, if it would otherwise be None.

>>> class Person(GraphObject):
...     name = Property()
>>> alice = Person()
>>> alice.name = "Alice Smith"
>>> alice.name
"Alice Smith"

4.3. Labels

A Label defined on a GraphObject provides an accessor to a label of the underlying central node. It is exposed as a boolean value, the setting of which allows the label to be toggled on or off.

Labels are exposed in the API in an identical way to boolean properties. The difference between these is in how the value translates into the graph database. A label should generally be used if matches are regularly carried out on that value. Secondary or supporting information could be stored in a boolean property.

class py2neo.ogm.Label(name=None)[source]

Label definition for a GraphObject.

Labels are toggleable tags applied to an object that can be used as type information or other forms of classification.

>>> class Food(GraphObject):
...     hot = Label()
>>> pizza = Food()
>>> pizza.hot
>>> pizza.hot = True
>>> pizza.hot

4.5. Object Matching

One or more GraphObject instances can be selected from the database by using the match method of the relevant subclass.

To select a single instance using the primary label and primary key:

>>> Person.match(graph, "Keanu Reeves").first()
<Person name='Keanu Reeves'>

To select all instances that match certain criteria, you can simply iterate through the Match object. Note the use of the underscore in the Cypher WHERE clause to refer to the underlying node:

>>> list(Person.match(graph).where("_.name =~ 'K.*'"))
[<Person name='Keanu Reeves'>,
 <Person name='Kevin Bacon'>,
 <Person name='Kiefer Sutherland'>,
 <Person name='Kevin Pollak'>,
 <Person name='Kelly McGillis'>,
 <Person name='Kelly Preston'>]
class py2neo.ogm.GraphObjectMatch(graph, labels=frozenset({}), conditions=(), order_by=(), skip=None, limit=None)[source]

A selection of GraphObject instances that match a given set of criteria.


Iterate through items drawn from the underlying graph that match the given criteria.


Return the first item that matches the given criteria.

4.6. Object Operations

GraphObject instances can be pushed into and pulled from the database just like other py2neo objects. Unlike with other kinds of object though, a GraphObject can be pushed without having first created or merged it. For example:

 >>> alice = Person()
 >>> alice.name = "Alice Smith"
 >>> graph.push(alice)
 >>> alice.__node__
(_123:Person {name: 'Alice Smith'})

Update a GraphObject and its associated RelatedObject instances with changes from the graph.


Update the graph with changes from a GraphObject and its associated RelatedObject instances. If a corresponding remote node does not exist, one will be created. If one does exist, it will be updated. The set of outgoing relationships will be adjusted to match those described by the RelatedObject instances.


For a GraphObject, create and merge are an identical operation. This is because GraphObject instances have uniqueness defined by their primary label and primary key and so both operations can be considered a form of merge.

If a corresponding remote node does not exist, one will be created. Unlike push, however, no update will occur if a node already exists.


Delete the remote node and relationships that correspond to the given GraphObject.