Working with Neo4j with Django neomodel library with Dara Silvera

This video features Dara Silvera at DjangoCon US 2023 in Durham, North Carolina, USA.

Working with Neo4j with Django neomodel library with Dara Silvera
0:17:29
Published November 22, 2023
1,433 views

In this talk, we will explore how to work with Neo4j using Python to build scalable and efficient web applications. When we think of databases, the first thing that comes to mind are relational databases like MySQL or PostgreSQL, but there are other types of databases, such as non-relational databases, which are very useful and efficient for various problems. An example of one of these non-relational databases is a graph database, such as Neo4j

We will learn how graph databases work, the syntax of Neo4j, how to transform a relational model into a non-relational one with simple steps, and examples of queries using Cypher. We will also take a look at use case scenario using the Django neomodel library. a basic API using Django with the interface neomodel to connect with Neo4j By the end of the talk, attendees will have a basic understanding of how to leverage these technologies to build fast and scalable web applications.

This talk was presented at: https://2023.djangocon.us/talks/working-with-neo4j-with-django-neomodel-library/

LINKS:
Follow Dara Silvera 👇

Follow DjangCon US 👇
https://fosstodon.org/@djangocon
https://twitter.com/djangocon

Follow DEFNA 👇
https://www.defna.org/

Video production by the presenter and DjangoCon US 2023 volunteers.

Summary

Graph databases are useful when the important part of the data is how entities connect, especially for complex relationships, hierarchies, evolving schemas, recommendations, fraud detection, and real-time analysis. Dara Silvera explains Neo4j’s property-graph model, Cypher pattern-matching queries, variable-length path searches, and algorithms for centrality, community detection, and path finding. She then shows how neomodel brings Django-style model definitions and query methods to Neo4j, using a Panama Papers-style data model and examples that combine the neomodel API with raw Cypher. The central argument is that Neo4j makes relationship-heavy data easier to explore and can reveal connections that would be difficult to find with relational queries alone.

Key takeaways

  • Graph databases store relationships as first-class data, making them well suited to highly connected, hierarchical, or frequently changing data.
  • Neo4j’s property-graph model represents nodes with labels and properties and relationships with types, directions, and optional properties.
  • Cypher uses readable pattern matching and supports filtering, sorting, limits, and variable-length path queries.
  • Neo4j provides graph algorithms such as centrality, community detection, and path finding for deeper analysis.
  • Neomodel offers Django-like model definitions and query methods while still allowing developers to execute Cypher directly.
  • A Panama Papers-style graph can expose indirect connections and shortest paths between entities that would be difficult to discover with SQL joins.

Summarised automatically from the transcript.

Transcript

2,060 words · auto-generated Show

Automatically transcribed, so expect mistakes in names and technical terms.

0:21

Um well, hello everybody. And so today is my first time in the Shangokon. So this talk is about NeoProche with Django and Neo Model Library. So let's start. So it's easy to come across with use cases where it could be useful to use graph database. And we don't realize since we are used to other types of database. The idea of this talk is to present some examples of when to make sense to use to use it and incorporate it with our Django backend using Neo Model Library.

1:10

So first of all, I'm going to talk about me. My name is Dara Silvera. I'm a full stack developer at Octobot. which is a software company with nine years of experience. I'm from Montevideo, Uruguay. It's a small country in South America between Brazil and Argentina. I'm a huge fan of Formula One. I love rabbits traveling. Oh yes, we have fun of Formula One here. And I'm kind of a makeup artist. And this is my QR of LinkedIn if you want to connect. So we have SQL database and no SQL database listed here.

1:56

When you have a problem, your mind is just trying to go to a relational database. Even though they are called relational, they don't handle relationships very well. For no SQL we have three um we have four um no SQL Database listed here, three of them are don't have the concept of relationship, but each one is created to resolve a particular problem. Then we have the graph database and there we then uh we have Neil4. This captures the relationship as part of the data

2:43

Sorry. Sorry for that. Okay, so Neo4chet as an FD Graph database. The connections are already there, stored right in the database. Native technologies tends to perform faster, scale bigger and run more efficiently, calling for a much lower hardware requirement. This capability to transverse data without the index lookup makes it more efficient. So what that looks like. The data model for Net4G is something called property graph. Apart from defining the nodes that indicates are person and car. and we have to define some attributes that

3:31

identify them. These attributes can be a string, a Boolean or a numeric. And for relationships we have to define exactly one type and one property that in this case we have loves and owns, for example, and we can have zero or more properties like for example the the model of the car the sorry um signs uh and the date of when it drives okay so When to use Graph database. First of all, some complex data relationships Some databases have relationships that are complex by the very nature.

4:20

An example of this is the is the Paras paper. It was an investigative journalism effort that exposed people who were using offshore financial instruments and accounts to hire different companies. So the people who were using this mechanism were trying to hide their wealth. Another example could be highly connected data where the grade of connected is very large. hierarchical data when for example we have a self-foreign key and it could be difficult if we try to um do a lot of joins Then involving schema, if our data structures is not well defined yet

5:05

and it will may suffer some changes in the future. quite um interesting for that because it's not uh so fixed. And then real-time insights. An example of this could be a recommendation system or prevention fraud payments. About complex data relationships, I'm gonna bring an example of this later. And for hierarchical data I have I bring a small example. So uh one powerful feature of Neil4 Shane is the ability to query for paths of arbitration

5:50

length. This allows us to find a connection between nodes that we don't know what the con and that they were there. or even without knowing the length of these connections. So for example, if we have this database when the labels are employee and the relationships are manage and we wanted to know other people that Shane managed, we can know this with this simple query without adding the the the maximum length of the path. So the the result of the query it will be it will be the same graph as here, but if we put for example

6:36

a three It will be from Shane to Shane because it will be three um relationships between them There are a lot of grass algorithms that we can use in Al-4G. I just bring three. The first one is centrality, that is used to determine the importance of distinct nodes in a network. Then we have community detection that are used to evaluate how groups of notes are cluster or partition it. Then we have path finding. They could be find the path between two or more nodes or evaluate if a path exists. And of course we can use more of

7:23

one of these algorithms together, so that's kind of interesting and give us more insights about our database. So let's talk about Cypher. Cipher is a high-level query language based on pattern matching It 's used to create nodes, update, and the date information and query graph. So for example for nodes we have the variable, the variable, the variable between parentheses and then we have to define the labels to identify them. And optionally we can add the properties. And then for edges, the edges have to be between brackets. in this example and between two nodes

8:10

and for example here in between n and b and we have to of course um add the direction between the this nodes And optionally we can add the properties to identifying this relationship too. So there are a lot of clauses in Cypher, but I'll bring some examples of them. For example, match is to specify the pattern to search for in the database The clauses were is to add contrast to the patterns. Otherwise is to specify that the output should be sorted, could be ascending or descending. Return defines what to include in the query result and limit is to constrain the number of rows in the output.

8:57

So it's readable and I think that if you read it it's you can understand the query. That's I think that's fantastic. And here it's a small example that we want to know all the people that then loves um so uh in this example um this is a variable that can be um for for example come he can love cards or whatever the the labels can be in this graph and we will return all the and will return all the per um the per the labels that he loves Well, so let's talk about

9:44

new model. What if we like our existing stack, but still want to benefit of a graph database? NoModel is an object graph maper for Ne4Shape graph database. It's familiar with Django model style definitions. So here is the shape of the data of Parada 's paper. So first we have the entity That is the offshore legal entity, this could be a company or a trust that was created in a low task jurisdiction Then we have an officer that's a person or a company who plays a role in an offshare entity, such a beneficiary or a shareholder. And then we have the intermediary

10:29

that is a go between an offshore corporation and an offshore um Sorry, uh an offshore uh corporation and a person that wants it. And then we have uh address that are all the address that was found in the IC IG database and others are other entities that plays a role in this database. So these are all the possible relationships, but this I only used a pair of them. So for example, for entity , we have like

11:15

the relationships that this can be defined using relationship from, relationship to, or relationships. So as I said before, it's quite uh similar to how we work in Python. I mean nodes are defined in the same way. and classes are defined in Python with the only difference that we must define as neomodal property objects. And we have to also define the what is um which is gonna be the ID. And this example is not ID And we just write unique Idrib property. And this is the same but for officer. So if we see here

12:00

for entities we have a relationship to And this is the name of the relationship. Here we import the model. So we are an officer. We have entities and relationship to. But if we go back from to entity, we have officer that has a relationship from and it's the same name of of relationship. So here we have from officer to entity the relationship From entity and of course for every model we have to serialize the connections and this is how we serialize them, the relationships And this

12:45

a house uh this is how you and the database looks like This is a query made with cipher. For example, if we want to discover the shortest path between two officers. This is how can we do it? Here we have the name that contains Ross Junior. This is the null. And the other one is a contains grant. You have to believe me, this contains grants. And this is how we use all shortest paths. And this will give you all the shortest paths navigating with

13:31

these relationships and maximum length of 10 And then we return it. So it's quite interesting how we can it visualize the data. And Neo model contains an IP an API for querying sets of nodes without having to write cipher. So for example we have filter and the filter method borrows the same shango filter format. For example, less than, greater than Then we have get as the same as Django, has, that is to know if a node has a relationship or not, ordered by

14:17

to order by the query. And then if we want to query a more complex filter, we can import Q from NeoModel. And this could be an example of that, how to use it. And if we want to still use cipher, we can we have to import DV from the model and we have to write the query. This is the almost the same query between um if we're doing it with the APR or we are doing with cipher. And this is a more complex query that's the same that I showed you before.

15:04

I sh I brings uh just a part of the result. Um I think that's about neo model is that The query doesn't bring us all the nodes that are between these two nodes. Just bring us the first. The start node, so in this case is Ross Junior and then the end of the note that in this case is Bloomberg Anthony Grant And the size is the size between this path. So in this example is 6 So I think that's it's a work in progress traversing data and traversing the graph with new

15:49

model And a conclusion, uh, well, there are a lot of conclusions. Uh first of all, uh graph database are the worst way, are the best way to explore the relationships between entities It's much more uh intuitive to use this uh for this purpose than SQL database. Um and a person that was working with the Paradise papers said that if we were work if we were looking at the documents and we will never uh find out all these complex the rel relationships. So and Neo4Shape help for that. And of course the data visualization, I think that's great and that you can see

16:36

how your query is visualize. Of course different insights and as I say before if we use um this um If we use centrality or if we use pathfinding or of that, it can be give us um uh more powerful insights of our graph database. So I think that's all. I have to say thank you for hearing me and being today here. So and the questions will be addressed in the hallway. Thanks.

Questions this talk answers

What is Neo4j’s property graph model?

Neo4j represents data as nodes with labels and properties, connected by typed relationships that can also have properties. This lets the database store both entities and their connections as first-class data.

Discussed at 2:43

When should I use a graph database instead of a relational database?

Graph databases are useful for complex or highly connected relationships, hierarchical data, evolving schemas, and real-time insights such as recommendations or fraud prevention. Neo4j stores connections directly, making relationship traversal more efficient for these use cases.

Discussed at 3:31

What is Cypher and how do I query a Neo4j graph with it?

Cypher is Neo4j’s pattern-matching query language, used to create, update, and retrieve graph data. Nodes are written in parentheses and relationships in brackets, with clauses such as MATCH, WHERE, ORDER BY, RETURN, and LIMIT controlling the query.

Discussed at 7:23

How do I use Django-style models with Neo4j?

neomodel is an object graph mapper for Neo4j that uses definitions similar to Django models. Nodes are Python classes with neomodel properties, while relationships are declared explicitly in the model and serialized for database use.

Discussed at 9:44

How do I find the shortest path between two nodes in Neo4j?

Cypher’s allShortestPaths function can find every shortest path between two matched nodes, optionally limiting the maximum path length. The example searches between two officers and returns paths of up to ten relationships.

Discussed at 12:45

How can I query Neo4j with neomodel without writing Cypher?

neomodel provides a Django-like API for querying node sets, including filter, get, has, and order_by, as well as Q objects for more complex conditions. Cypher is still available when a query is too complex for the higher-level API.

Discussed at 13:31

Presenters

Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.

More videos from DjangoCon US