python - 以下模型与 django neomodel

标签 python django neo4j cypher graph-databases

我使用 neo4j 数据库和 django-neomodel, 我想为我的用户实现一个关注系统并为用户编写这个模型:

class UserNode(StructuredNode):
    user_id = IntegerProperty(required=True, index=True)
    follow = RelationshipTo('UserNode','FOLLOW')

    def get_followers(self):
        results, metadata = self.cypher("START a=node({self}) MATCH a<-[:FOLLOW]-(b) RETURN b");
        return [self.__class__.inflate(row[0]) for row in results]

    def get_following(self):
        results, metadata = self.cypher("START a=node({self}) MATCH b-[:FOLLOW]->(a) RETURN b");
        return [self.__class__.inflate(row[0]) for row in results]

    def follow_person(self, user_id):
        import datetime
        from django.utils.timezone import utc

        followed_user = self.index.get(user_id=user_id)
        self.follow.connect(followed_user, {'time': str(datetime.datetime.utcnow().replace(tzinfo=utc))})
        self.save()
        followed_user.save()

您认为我的以下系统的 UserNode 模型是一个好的模型吗?

这个模型出现了问题! 当我运行这段代码时:

a = UserNode.index.get(user_id=200)
b = UserNode.index.get(user_id=201)
c = UserNode.index.get(user_id=202)

a.follow_person(201)
b.follow_person(200)

print a.get_followers(), a.get_following()

输出是两个空列表。为什么?

最佳答案

你好,这里是 neomodel 的作者。

follow_person 方法中不需要保存。 您使用的是哪个版本的新模型?以下代码对我有用:

class UserNode(StructuredNode):                                                                                                                                                                        
    user_id = IntegerProperty(required=True, index=True)
    follow = RelationshipTo('UserNode', 'FOLLOW')

    def get_followers(self):
        results, metadata = self.cypher("START a=node({self}) MATCH a<-[:FOLLOW]-(b) RETURN b")
        return [self.__class__.inflate(row[0]) for row in results]

    def get_following(self):
        results, metadata = self.cypher("START a=node({self}) MATCH b-[:FOLLOW]->(a) RETURN b")
        return [self.__class__.inflate(row[0]) for row in results]

    def follow_person(self, user_id):
        followed_user = self.index.get(user_id=user_id)
        self.follow.connect(followed_user, {'time': str(datetime.datetime.utcnow())})

u1 = UserNode(user_id=1).save()
u2 = UserNode(user_id=2).save()

u1.follow_person(2)
u2.follow_person(1)
print "User 1 follows {}".format(u1.get_following())
print "User 1's followers {}".format(u1.get_followers())

您可能还想考虑为传出和传入定义两个单独的关系管理器,这意味着您不需要编写密码查询:

class UserNode(StructuredNode):
    user_id = IntegerProperty(required=True, index=True)
    following = RelationshipTo('UserNode', 'FOLLOW')
    followers = RelationshipFrom('UserNode', 'FOLLOW')

    def follow_person(self, user_id):
        followed_user = self.index.get(user_id=user_id)
        self.follow.connect(followed_user, {'time': str(datetime.datetime.utcnow())})

print "User 1 follows {}".format(u1.following.all())
print "User 1's followers {}".format(u1.followers.all())

希望对您有所帮助!如果您还有任何问题,请随时给我发电子邮件。

关于python - 以下模型与 django neomodel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16887645/

相关文章:

python - 在 Bluehost 上安装 Django

python - 允许用户下载在 AWS 上生成的 zip 文件

neo4j - 如何在neo4j中使用cypher创建多个节点

neo4j - 有没有办法将多个 WHERE 语句链接在一起而无需在 Neo4j 中切换到 AND?

python - 如何检查 xml 中两个元素的属性值是否相同

Python - 如何在每 3 个字符上添加空格?

python - 如何将 Tkinter 小部件设置为等宽、平台无关的字体​​?

Django vs. Grok/Zope3 vs. Pylons

python - 如何使用 Tox 和 Poetry 在 CircleCI 中设置多个解释器?

linux - Neo4j 警告 : Max 1024 open files allowed, 建议至少 40 000。请参阅 Neo4j 手册