py2neo and object model

600 views
Skip to first unread message

Shekar Tippur

unread,
Jun 14, 2015, 2:03:52 PM6/14/15
to ne...@googlegroups.com


Hello,

I am trying to use py2neo (http://py2neo.org/2.0/ext/ogm.html).
I am having trouble looking up a node.

I already have a node stored. Fruit("mango")

I am trying to add a User("Shekar") to like Fruit("mango")

I am able to add 2 nodes but I am having trouble creating relationship.

What is the best way to get a node given the fruit name, desrialize it to something that can be a added as a relation?

- Shekar

Sumit Gupta

unread,
Jun 14, 2015, 11:40:21 PM6/14/15
to ne...@googlegroups.com
Hi,

You can directly execute the Cypher Query and get the data ans then create the relationship.


Thanks,
Sumit

Shekar Tippur

unread,
Jun 15, 2015, 3:48:01 AM6/15/15
to ne...@googlegroups.com
Thanks Sumit,

I think the issue maybe more fundamental.

Here is my model.

from django.db import models
from py2neo import Graph
from py2neo.ext.ogm import Store
import datetime

graph = Graph()
store = Store(graph)


class Fruit(object):
FruitName = models.CharField(max_length=100)
cr_date = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('FruitName',)

def __init__(self, FruitName=None, cr_date=None):
print ("Here in model")
self.FruitName = GlobalCategoryName
self.cr_date = cr_date or datetime.datetime.now()
store.save_unique("Fruit","FruitName",FruitName,self)


def __str__(self):
return self.FruitName

When I run this, an object gets created with attributes name and creation date but the object is not of type fruit. Wondering what I am doing wrong.

- Shekar

Sumit Gupta

unread,
Jun 15, 2015, 8:51:52 AM6/15/15
to ne...@googlegroups.com
"object is not of type fruit" - What do you mean by that? Are you comparing this with ORM where you define your own Entities and every entity has correspnding tables?

if yes, then please do not do that. Neo4j provides only Object Type = "Node", "Relationship", Label, Property/ Attribute.

Thanks,
Sumit 

On Sunday, 14 June 2015 23:33:52 UTC+5:30, Shekar Tippur wrote:

Nigel Small

unread,
Jun 15, 2015, 9:01:05 AM6/15/15
to Neo4j
Important note: The py2neo OGM module is old and works only with legacy indexes, not schema indexes or any other Neo4j 2.0 concepts. Therefore, if "of type" refers to labels, that's why.

You have only one OGM action in the code you've shared and this is store.save_unique. If you have an entry in your legacy index following execution of this line then it has done everything it should do. If you want a different action to take place, please clarify what that is.

Regards
Nigel

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shekar Tippur

unread,
Jun 15, 2015, 11:51:47 AM6/15/15
to ne...@googlegroups.com
Nigel,
If  py2neo OGM module is old. what is the most recommended way to integrate neo4j/python. Looks like there are couple of modules that are in this state.

http://neo4j.com/developer/python/ I am following this document. I have been shot down on py2neo OGM and https://neo4django.readthedocs.org/en/latest/getting-started.html - neo4django.

- Shekar

Shekar Tippur

unread,
Jun 15, 2015, 1:27:46 PM6/15/15
to ne...@googlegroups.com
Sumit,

I am trying to simulate:
CREATE (matrix1:Movie { title : 'The Matrix', year : '1999-03-31' })

but what I am getting is

CREATE ( { title : 'The Matrix', year : '1999-03-31' })

Sumit Gupta

unread,
Jun 15, 2015, 8:37:48 PM6/15/15
to ne...@googlegroups.com
try this piece of code snippet: -

    node1 = Node("FirstLabel", "SecondLabel",name="MyPythonNode2", neo4j_version="2.2")

    resultNodes = graph.create(node1)

    #Iterate Over Tuple and print all the values in the Tuple

    for index in range(len(resultNodes)):

        print("Created Node - ", index, ", ", resultNodes[index])

    

The above piece of code will create a Node with 2 Labels and 2 properties.


Thanks,
Sumit

Scott Werner

unread,
Jun 16, 2015, 3:48:26 PM6/16/15
to ne...@googlegroups.com
You could try using neomodel - https://github.com/robinedwards/neomodel.

If that doesn't provide what you need, I use py2neo's Node and Relationship objects with marshmallow - https://marshmallow.readthedocs.org/ for object serialization and deserialization. 

Shekar Tippur

unread,
Jun 17, 2015, 2:17:07 AM6/17/15
to ne...@googlegroups.com
Sumit,

That worked.

Thanks,
Shekar

Antonina Panayiotou

unread,
Feb 21, 2017, 5:11:02 AM2/21/17
to Neo4j
Hello Nigel. I'm trying to make the example and either i have it as "from py2neo.ext.ogm import Store" or "from flask.ext.neo4j import Neo4j" it throws me the exception
"from py2neo.ext.ogm import Store
ImportError: No module named ogm "
Do you know how to use the ogm extension? i'm working with python2.7 and py2neo

Nigel Small

unread,
Feb 21, 2017, 7:28:22 AM2/21/17
to Neo4j
Hey Antonina

I'm guessing that you're looking at documentation from an earlier version of py2neo. The OGM module has been rewritten in v3 to take advantage of labels (the old one used manual indexes instead) and can now be found at py2neo.ogm. You can read up on it here -> http://py2neo.org/v3/ogm.html

Cheers
Nigel

To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages