具有多个数据库Mongodb和Neo4j的Django

时间:2018-10-19 14:02:25

标签: django python-3.x mongodb neo4j neomodel

我使用 Mongo 作为我的主数据库,并使用 Neo4j 存储一些关系。希望Neo4j可以减少我的应用程序中复杂搜索的查询时间。如何维护两者之间的关系。

这是我的问题在这种情况下如何在两个不同数据库的表之间建立关系

我正在使用 Python3.6,Django2.1 ,django-neomodel 0.0.4和Djongo 1.2.30

这是我的models.py示例:

class Listing(models.Model):
''' Listing Model for mongo database '''
create_time = models.DateTimeField()
category = models.EmbeddedModelField(
    model_container=Category,
)
subcategory = models.EmbeddedModelField(
    model_container=Subcategory,
    model_form_class=SubcategoryForm
)
...


class Listingnode(DjangoNode):
    uid = UniqueIdProperty()
    list_id = StringProperty()
    status = StringProperty()
    created = DateTimeProperty(default=datetime.utcnow)
    price_range = RelationshipTo('PricerangeNodes','PRICE_RANGE')
    tags = RelationshipTo('TagNodes','TAGS')

1 个答案:

答案 0 :(得分:1)

您可以将自动生成的属性id添加到MongoDB实体以及Neo4j实体,将id存储在分别链接的其他实体中,并通过对象图加载对象必要时,通过存储的id映射库(neo4j-ogm)。

1。 MongoDB部分(Java版本)

1.1 YourMongoEntity

@Document
public class YourMongoEntity {
  @Id
  private String id;

  @Indexed(unique = true)
  private String furtherIdentifier;

  // For reasons of clarity the default constructor, getter and setter are omitted.  
}

1.2 YourMongoEntityDAO

@Repository
public interface YourMongoEntityDAO extends MongoRepository<YourMongoEntity, String> {
  YourMongoEntity findById(String id);
}

2。 Neo4j部件(Java版)

2.1 YourNeo4jEntity

@NodeEntity
public class YourNeo4jEntity  {
  @Id
  @GeneratedValue
  private Long id;

  @Index(unique = true)
  private Long furtherIdentifier;

  // For reasons of clarity the default constructor, getter and setter are omitted.  
}

2.2 YourNeo4jEntityDAO

@Repository
public interface YourNeo4jEntityDAO extends Neo4jRepository<YourNeo4jEntity, Long> {
  YourNeo4jEntity findId(Long id);
}