Fixed querying string versions of ObjectIds issue with ReferenceField (#307)

This commit is contained in:
Ross Lawley 2013-05-07 11:54:47 +00:00
parent 7cde979736
commit 9dfee83e68
4 changed files with 49 additions and 4 deletions

View File

@ -4,6 +4,7 @@ Changelog
Changes in 0.8.0 Changes in 0.8.0
================ ================
- Fixed querying string versions of ObjectIds issue with ReferenceField (#307)
- Added $setOnInsert support for upserts (#308) - Added $setOnInsert support for upserts (#308)
- Upserts now possible with just query parameters (#309) - Upserts now possible with just query parameters (#309)
- Upserting is the only way to ensure docs are saved correctly (#306) - Upserting is the only way to ensure docs are saved correctly (#306)

View File

@ -854,7 +854,7 @@ class ReferenceField(BaseField):
return document.id return document.id
return document return document
elif not self.dbref and isinstance(document, basestring): elif not self.dbref and isinstance(document, basestring):
return document return ObjectId(document)
id_field_name = self.document_type._meta['id_field'] id_field_name = self.document_type._meta['id_field']
id_field = self.document_type._fields[id_field_name] id_field = self.document_type._fields[id_field_name]

View File

@ -544,8 +544,9 @@ class QuerySet(object):
return c return c
def select_related(self, max_depth=1): def select_related(self, max_depth=1):
"""Handles dereferencing of :class:`~bson.dbref.DBRef` objects to """Handles dereferencing of :class:`~bson.dbref.DBRef` objects or
a maximum depth in order to cut down the number queries to mongodb. :class:`~bson.object_id.ObjectId` a maximum depth in order to cut down
the number queries to mongodb.
.. versionadded:: 0.5 .. versionadded:: 0.5
""" """

View File

@ -1321,6 +1321,49 @@ class QuerySetTest(unittest.TestCase):
self.Person.objects()[:1].delete() self.Person.objects()[:1].delete()
self.assertEqual(1, BlogPost.objects.count()) self.assertEqual(1, BlogPost.objects.count())
def test_reference_field_find(self):
"""Ensure cascading deletion of referring documents from the database.
"""
class BlogPost(Document):
content = StringField()
author = ReferenceField(self.Person)
BlogPost.drop_collection()
self.Person.drop_collection()
me = self.Person(name='Test User').save()
BlogPost(content="test 123", author=me).save()
self.assertEqual(1, BlogPost.objects(author=me).count())
self.assertEqual(1, BlogPost.objects(author=me.pk).count())
self.assertEqual(1, BlogPost.objects(author="%s" % me.pk).count())
self.assertEqual(1, BlogPost.objects(author__in=[me]).count())
self.assertEqual(1, BlogPost.objects(author__in=[me.pk]).count())
self.assertEqual(1, BlogPost.objects(author__in=["%s" % me.pk]).count())
def test_reference_field_find_dbref(self):
"""Ensure cascading deletion of referring documents from the database.
"""
class BlogPost(Document):
content = StringField()
author = ReferenceField(self.Person, dbref=True)
BlogPost.drop_collection()
self.Person.drop_collection()
me = self.Person(name='Test User').save()
BlogPost(content="test 123", author=me).save()
self.assertEqual(1, BlogPost.objects(author=me).count())
self.assertEqual(1, BlogPost.objects(author=me.pk).count())
self.assertEqual(1, BlogPost.objects(author="%s" % me.pk).count())
self.assertEqual(1, BlogPost.objects(author__in=[me]).count())
self.assertEqual(1, BlogPost.objects(author__in=[me.pk]).count())
self.assertEqual(1, BlogPost.objects(author__in=["%s" % me.pk]).count())
def test_update(self): def test_update(self):
"""Ensure that atomic updates work properly. """Ensure that atomic updates work properly.
""" """