【问题标题】:neomodel doesn't save nodes in databaseneomodel 不在数据库中保存节点
【发布时间】:2019-08-23 03:33:30
【问题描述】:

我有一个新模型:

class User(StructuredNode):
    id = UniqueIdProperty()
    username = StringProperty(unique_index=True)
    email = StringProperty(unique_index=True)
    password = StringProperty()

    def save(self, *args, **kwargs):
        self.password = make_password(self.password)
        return super(User, self).save(*args, **kwargs)

当我在控制台中保存此类模型的新实例时,我成功获得了一个新的用户实例,但是当我从模型中检索所有 User 对象时,我得到一个空结果集:

(AttractoraVenv) MacBook-Pro-de-Hugo:AttractoraBackend hugovillalobos$ python manage.py shell
Python 3.7.1 (default, Dec 14 2018, 13:28:58)
[Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from users.models import User
>>> user = User(username='admin', email='admin@admin.com', password='secret').save()
>>> user
<User: {'id': '0d3b0aa56499414d8fb205ea1279662e', 'username': 'admin', 'email': 'admin@admin.com', 'password': 'pbkdf2_sha2
56$150000$vTdAnqAvXnOM$0hPMc1KiMpLvHbVO4C77OXUa9dmKzt9lMxIg8Ig+za8='}>
>>> User.nodes.all()
[]

我不知道我错过了什么。

编辑

我发现当我使用 neo4j 控制台中的密码创建用户实例时,User.nodes.all() 会返回这样的实例。所以我想问题出在User.save()。我还发现User.save()在更新的时候效果很好,所以问题出在create上。

【问题讨论】:

    标签: django neo4j neomodel


    【解决方案1】:

    id 是一个内置属性,重写它不是一个好主意。您可以将id = UniqueIdProperty() 替换为userid = UniqueIdProperty() 之类的内容,这样应该可以解决问题。

    【讨论】:

    • 你能链接说 id 是内置的文档吗?我刚遇到这个问题,我花了一段时间才找到这篇文章。
    【解决方案2】:

    我不确定重写 save 方法是否是好方法。

    试试这个(完全删除save 方法):

    class User(StructuredNode):
        id = UniqueIdProperty()
        username = StringProperty(unique_index=True)
        email = StringProperty(unique_index=True)
        password = StringProperty()
    
        def pre_save(self):
            self.password = make_password(self.password)
    

    【讨论】:

    • 不,完全一样:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多