Skip to content

Instantly share code, notes, and snippets.

@szabi
Last active December 18, 2015 15:18
Show Gist options
  • Save szabi/5802873 to your computer and use it in GitHub Desktop.
Save szabi/5802873 to your computer and use it in GitHub Desktop.
This is the strange situation described in http://stackoverflow.com/questions/17126274/objects-get-does-not-work-as-expected . See file `output` including its comments to have the extremely confusing behaviour demonstrated. See the Gist notes for version information and further explanation.
# from uuslug import uuslug
from neo4django.db import models
from neo4django.auth.models import User
# Create your models here.
class Category(models.NodeModel):
name = models.StringProperty(unique=True, indexed=True)
url_name = models.StringProperty(unique=True, indexed=True)
description = models.StringProperty()
class Monument(models.NodeModel):
name = models.StringProperty(unique=True, indexed=True)
sigil = models.StringProperty(unique=True, indexed=True)
class Glyph(models.NodeModel):
name = models.StringProperty(unique=True, indexed=True)
class Character(models.NodeModel):
name = models.StringProperty(unique=True, indexed=True)
unicode_name = models.StringProperty(unique=True, indexed=True)
class Review(models.NodeModel):
of_literature = models.Relationship('Review', rel_type='REVIEW', related_name='reviews', single=True)
uri = models.StringProperty()
by = models.Relationship(User, rel_type='BY', related_name='reviews', single=True)
class Person(models.NodeModel):
surname = models.StringProperty(required=True, indexed=True)
given_name = models.StringProperty(required=True, indexed=True)
born = models.DateProperty()
died = models.DateProperty()
name_slug = models.StringProperty(indexed=True, required=True)
bio_uri = models.StringProperty()
# def save(self, *args, **kwargs):
# self.name_slug = uuslug(" ".join([self.surname,self.given_name]), instance=self, slug_field='name_slug')
# super(Person, self).save(*args, **kwargs)
class AuthorAlias(models.NodeModel):
surname = models.StringProperty(required=True, indexed=True)
given_name = models.StringProperty(required=True, indexed=True)
identity=models.Relationship(Person,rel_type='ALIAS',single=True, related_name='aliases')
class Literature(models.NodeModel):
title = models.StringProperty()
authors = models.Relationship('AuthorAlias',rel_type='AUTHOR', related_name='works')
pub_year = models.IntegerProperty()
pub_place = models.StringProperty()
abstract_uri = models.StringProperty()
external_ref = models.URLProperty()
references=models.Relationship('Literature', rel_type='REFERENCE', related_name='referred_by')
# reviews
#reliability
categories = models.Relationship(Category, rel_type='CATEGORY', related_name='members')
monuments = models.Relationship(Monument, rel_type='DEALTS_WITH', related_name='literature')
glyphs = models.Relationship(Glyph, rel_type='DEALS_WITH', related_name='literature')
characters = models.Relationship(Character, rel_type='DEALS_WITH', related_name='literature')
([virtualenv])07:15@[hostname]:~/[path_to_project] ? python manage.py shell
Python 2.7.5 (default, Jun 2 2013, 17:15:36)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import lit.models as appModel
/[...]/src/neo4django/neo4django/db/models/relationships.py:165: UserWarning: `glyphs` and `characters` share a relationship type and direction. Is this what you meant to do?
% (r.name, name))
>>> import test_models_complete as testModelC
>>> # BTW: Strangely, even though this file is an exact copy of the app's model (except for the included `Meta`s), the above (unwarranted) warning does not appear
>>> import test_models as testModel
>>>
>>> # There is only one person, so an empty `.get()` *does* return a value
>>> slug=appModel.Person.objects.get().name_slug
>>> slug
u'john-doe'
>>> # OK, worked fine
>>>
>>> # First: look at the model imported from the app
>>> appModel.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.
>>> # => Error
>>>
>>> # Second: look at the model which is separated out from the app, but is otherwise a complete copy of the app's model
>>> testModelC.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.
>>> # => Error
>>>
>>> # Last, look at the model, which is an extract of the above models, containing _only_ `Person`. Otherwise unchanged, letter-for-letter the same (except for the `Meta.app_label`)
>>> testModel.Person.objects.get(name_slug=slug)
<Person: Person object>
>>> # => Works!
>>>
>>> # How can that be?
>>>
# from uuslug import uuslug
from neo4django.db import models
# from neo4django.auth.models import User
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)
born = models.DateProperty()
died = models.DateProperty()
name_slug = models.StringProperty(indexed=True, required=True)
bio_uri = models.StringProperty()
# def save(self, *args, **kwargs):
# self.name_slug = uuslug(" ".join([self.surname,self.given_name]), instance=self, slug_field='name_slug')
# super(Person, self).save(*args, **kwargs)
# from uuslug import uuslug
from neo4django.db import models
from neo4django.auth.models import User
# Create your models here.
class Category(models.NodeModel):
class Meta:
# since test_models isn't in an app
app_label='test_complete'
name = models.StringProperty(unique=True, indexed=True)
url_name = models.StringProperty(unique=True, indexed=True)
description = models.StringProperty()
class Monument(models.NodeModel):
class Meta:
# since test_models isn't in an app
app_label='test_complete'
name = models.StringProperty(unique=True, indexed=True)
sigil = models.StringProperty(unique=True, indexed=True)
class Glyph(models.NodeModel):
class Meta:
# since test_models isn't in an app
app_label='test_complete'
name = models.StringProperty(unique=True, indexed=True)
class Character(models.NodeModel):
class Meta:
# since test_models isn't in an app
app_label='test_complete'
name = models.StringProperty(unique=True, indexed=True)
unicode_name = models.StringProperty(unique=True, indexed=True)
class Review(models.NodeModel):
class Meta:
# since test_models isn't in an app
app_label='test_complete'
of_literature = models.Relationship('Review', rel_type='REVIEW', related_name='reviews', single=True)
uri = models.StringProperty()
by = models.Relationship(User, rel_type='BY', related_name='reviews', single=True)
class Person(models.NodeModel):
class Meta:
# since test_models isn't in an app
app_label='test_complete'
surname = models.StringProperty(required=True, indexed=True)
given_name = models.StringProperty(required=True, indexed=True)
born = models.DateProperty()
died = models.DateProperty()
name_slug = models.StringProperty(indexed=True, required=True)
bio_uri = models.StringProperty()
# def save(self, *args, **kwargs):
# self.name_slug = uuslug(" ".join([self.surname,self.given_name]), instance=self, slug_field='name_slug')
# super(Person, self).save(*args, **kwargs)
class AuthorAlias(models.NodeModel):
class Meta:
# since test_models isn't in an app
app_label='test_complete'
surname = models.StringProperty(required=True, indexed=True)
given_name = models.StringProperty(required=True, indexed=True)
identity=models.Relationship(Person,rel_type='ALIAS',single=True, related_name='aliases')
class Literature(models.NodeModel):
class Meta:
# since test_models isn't in an app
app_label='test_complete'
title = models.StringProperty()
authors = models.Relationship('AuthorAlias',rel_type='AUTHOR', related_name='works')
pub_year = models.IntegerProperty()
pub_place = models.StringProperty()
abstract_uri = models.StringProperty()
external_ref = models.URLProperty()
references=models.Relationship('Literature', rel_type='REFERENCE', related_name='referred_by')
# reviews
#reliability
categories = models.Relationship(Category, rel_type='CATEGORY', related_name='members')
monuments = models.Relationship(Monument, rel_type='DEALTS_WITH', related_name='literature')
glyphs = models.Relationship(Glyph, rel_type='DEALS_WITH', related_name='literature')
characters = models.Relationship(Character, rel_type='DEALS_WITH', related_name='literature')
@szabi
Copy link
Author

szabi commented Jun 18, 2013

Using:

  • neo4j 1.9
  • django 1.4.5
  • neo4django master from GitHub as of 2013-06-16.

This is the strange situation described in http://stackoverflow.com/questions/17126274/objects-get-does-not-work-as-expected . See file output including its comments to have the extremely confusing behaviour demonstrated.

Note, that test_models_complete.py is the exact copy of lit_models.py (lit_ in the file filename stands for lit/ IRL) AND that test_models.py is an exact copy of the Person class, without further modifications (except the Meta classes which are needed to run the files outside of an app context).

@szabi
Copy link
Author

szabi commented Jun 18, 2013

(Note to self: I commented out the uuslug dependency, as in the current code it's not needed. The corresponding passages were commented out in the models in the original Gist as well).

@nikolasd
Copy link

I had a similar problem (check my stackoverflow question here) that got resolved after I upgraded to Django 1.5.1 (keeping the same Neo4J version)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment