Merge pull request #326 from wpjunior/fixes-325

Fixes for #325 issue - Thanks @wpjunior
This commit is contained in:
Ross Lawley 2011-10-27 00:33:36 -07:00
commit c42aef74de
2 changed files with 20 additions and 0 deletions

View File

@ -666,6 +666,9 @@ class ReferenceField(BaseField):
return pymongo.dbref.DBRef(collection, id_)
def prepare_query_value(self, op, value):
if value is None:
return None
return self.to_mongo(value)
def validate(self, value):
@ -743,6 +746,9 @@ class GenericReferenceField(BaseField):
return {'_cls': document._class_name, '_ref': ref}
def prepare_query_value(self, op, value):
if value is None:
return None
return self.to_mongo(value)

View File

@ -992,15 +992,29 @@ class FieldTest(unittest.TestCase):
class Company(Document):
name = StringField()
Product.drop_collection()
Company.drop_collection()
ten_gen = Company(name='10gen')
ten_gen.save()
mongodb = Product(name='MongoDB', company=ten_gen)
mongodb.save()
me = Product(name='MongoEngine')
me.save()
obj = Product.objects(company=ten_gen).first()
self.assertEqual(obj, mongodb)
self.assertEqual(obj.company, ten_gen)
obj = Product.objects(company=None).first()
self.assertEqual(obj, me)
obj, created = Product.objects.get_or_create(company=None)
self.assertEqual(created, False)
self.assertEqual(obj, me)
def test_reference_query_conversion(self):
"""Ensure that ReferenceFields can be queried using objects and values
of the type of the primary key of the referenced object.