Skip to content

Integrace s Neo4j

Jan Koprajda requested to merge michal/graph-integration into master

Created by: msgre

Synchronizace Django objektu do databaze Neo4j.

Modely

Napsal jsem obecnou mixin tridu. Kazdy model, ktery se bude chtit synhronizovat do Neo4j bude muset mit jako predka tridu GraphModel, a uvnitr pretizenou metodu graph_data ktera vrati dict s daty:

from django.db import models
from django_extensions.db.models import TimeStampedModel
from common.models import GraphModel

class Manufacturer(TimeStampedModel, GraphModel):
    name = models.CharField(max_length=100)

    def graph_data(self):
        return {
            'name': self.name,
        }

Pokud ma model nejake relace (1:1, 1:N, M:M), je nutne do tridy pridat i popis toho, jak se ma relace namapovat do Neo4j:

class Car(TimeStampedModel, GraphModel):
    model = models.CharField(max_length=100)
    manufacturer = models.ForeignKey(Manufacturer, on_delete=models.SET_NULL, null=True)

    def graph_data(self):
        return {
            'name': self.name,
        }

    def related_graph_data(self):
        return [
            # Car - BELONGS_TO -> Manufacturer
            ("-", "BELONGS_TO", "->", self.manufacturer)
        ]

Admin

Aby se obsah Django modelu zrcadlil do Neo4j databaze i behem CRUD operaci provadenych v administracnim rozhrani, je treba pridat do definice admin tridy mixinu GraphAdmin:

from django.contrib import admin
from common.admin import GraphAdmin

class SubjectAdmin(GraphAdmin, admin.ModelAdmin):
    list_display = ("name", "code")

Management command

Command pro jednorazove preliti kompletniho datasetu z konkretnich modelu, napr:

./manage.py graph_sync colleges.College colleges.Faculty

Ukazka

Snímek obrazovky 2021-02-01 v 20 04 34

Konkretni relace mezi nody v Neo4j budeme muset doladit (upravou metod related_graph_data)

Merge request reports

Loading