2
votes

With the ogm module of py2neo you can build objects for nodes in neo4j:

class Person(GraphObject):
    __primarykey__ = 'name'

    name = Property()

    def __init__(self, name):
        self.name = name


peter = Person('peter')

graph.create(peter)

Is it possible to add dynamic properties to the Person object?

peter = Person('peter')

# this does not work
peter.last_name = 'jackson'

graph.create(peter)

It would be possible to first create a node and add properties later but it would be easier to create GraphObjects with dynamic properties.

1

1 Answers

1
votes

I came up with a kind of brute-force solution for this problem:

Rip out the class of your object, beat in the new property into the class and stuff it back into your object before it realizes what just happened :D

from py2neo.ogm import GraphObject, Property
from py2neo import Graph

class Person(GraphObject):
    __primarykey__ = "name"

    name = Property()

    def __init__(self, name):
        self.name = name

    def add_new_property(self, name, value):
        self.__class__ = type(
            type(self).__name__, (self.__class__,), {name: Property()}
        )
        setattr(self, name, value)


peter = Person("peter")
peter.add_new_property("lastname", "jackson")


g = Graph(host="localhost", user="neo4j", password="neo4j")
tx = g.begin()
tx.merge(peter)
tx.commit()

Works in this tiny lab setup. but should be tested in a more complex environment.

Cheers from the DZD :)

Tim