`objects.get(...)`不能按预期工作(`objects.get(…)` does no

2019-10-18 06:07发布

我试图使用neo4djangoNeo4j的数据库中获取对象

>>> # There is a single Person object in the database, so I get a value
>>> slug=Person.objects.get().name_slug
>>> print(slug)
doe-john
>>> # ok, it's there
>>> p=Person.objects.get(name_slug=slug)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/[...]/src/neo4django/neo4django/db/models/manager.py", line 37, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/[...]/lib/python2.7/site-packages/django/db/models/query.py", line 366, in get
    % self.model._meta.object_name)
DoesNotExist: Person matching query does not exist.
>>> p=Person.objects.get(name_slug__exact=slug)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/[...]/src/neo4django/neo4django/db/models/manager.py", line 37, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/[...]/lib/python2.7/site-packages/django/db/models/query.py", line 366, in get
    % self.model._meta.object_name)
DoesNotExist: Person matching query does not exist.

该错误消息是不理智的。 我刚刚收到的查询字符串从一外地,所以必须有一个相匹配。 有任何想法吗? 还是我偶然发现一个错误?

这真是奇怪,因为它与其他性质的工作,但不能与name_slug

>>> Person.objects.get(surname='Doe')
<Person: Person object>
>>> Person.objects.get(given_name='John')
<Person: Person object>
>>> Person.objects.get(name_slug='john-doe')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/[...]/src/neo4django/neo4django/db/models/manager.py", line 37, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/[...]/lib/python2.7/site-packages/django/db/models/query.py", line 366, in get
    % self.model._meta.object_name)
DoesNotExist: Person matching query does not exist.
>>> print( p.surname, p.given_name, p.name_slug )
(u'Doe', u'John', u'john-doe')

我的模型被定义为这样的:

class Person(models.NodeModel):
    surname = models.StringProperty(required=True, indexed=True)
    given_name = models.StringProperty(required=True, indexed=True)
    name_slug = models.StringProperty(indexed=True)

因此,唯一的区别是,它不是required ,但应该没有什么区别,在我的文档的理解。

Answer 1:

我不能复制这个使用neo4django高手,在Neo4j的1.9。

我创建了一个test_models.py

from neo4django.db import models

class Person(models.NodeModel):
    class Meta:
        # since test_models isn't in an app
        app_label='test'
    surname = models.StringProperty(required=True, indexed=True)
    given_name = models.StringProperty(required=True, indexed=True)
    name_slug = models.StringProperty(indexed=True)

然后跑了

>>> from test_models import Person
>>> john = Person.objects.create(surname=u'Doe', given_name=u'John', name_slug=u'john-doe')
>>> Person.objects.get(name_slug='john-doe')
<Person: Person object>
>>> john == Person.objects.get(name_slug='john-doe')
True
>>> jane = Person.objects.create(surname=u'Doe', given_name=u'Jane', name_slug=u'jane-doe')
>>> jane == Person.objects.get(name_slug='jane-doe')
True
>>> jane == Person.objects.get(given_name='Jane', surname='Doe')
True

思考?



文章来源: `objects.get(…)` does not work as expected