In this section, we will talk about executing the raw Cypher queries to handle complex requirements and then further converting them into a model and presenting it to the end user.
Cypher queries can be executed by the following two ways:
Using the
StructuredNode.cypher(query,params)
functionUsing the
neomodel.db.cypher_query(query, params)
function
In the first option, the query definition needs to be defined within the domain class itself and then it uses StructuredNode.inflate()
for creating domain objects from the raw Cypher results.
For example, let's assume that we need to retrieve and print all the male friends of a given node. So we will modify our Model.py
and add the following method in the Male
class:
def getAllMaleFriends(self): print('printing All Male friends') query = 'START a=node({self}) MATCH (a)-[:FRIEND]-(b:Male) RETURN b' results, columns = self.cypher(query) #Declare a List and then populate it with results maleList...