Skip to content

Commit d7fd6a4

Browse files
committed
Merge branch 'ref_field_to_mongo_bug' of https://github.com/yakxxx/mongoengine
Conflicts: AUTHORS
2 parents 9236f36 + 0da2dfd commit d7fd6a4

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,4 @@ that much better:
126126
* Garry Polley
127127
* Adrian Scott
128128
* Peter Teichman
129+
* Jakub Kot

mongoengine/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ def __get__(self, instance, owner):
778778
def to_mongo(self, document):
779779
if isinstance(document, DBRef):
780780
if not self.dbref:
781-
return DBRef.id
781+
return document.id
782782
return document
783783
elif not self.dbref and isinstance(document, basestring):
784784
return document

tests/test_fields.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,16 @@ class Person(Document):
11051105
p = Person.objects.get(name="Ross")
11061106
self.assertEqual(p.parent, p1)
11071107

1108+
def test_dbref_to_mongo(self):
1109+
class Person(Document):
1110+
name = StringField()
1111+
parent = ReferenceField('self', dbref=False)
1112+
1113+
p1 = Person._from_son({'name': "Yakxxx",
1114+
'parent': "50a234ea469ac1eda42d347d"})
1115+
mongoed = p1.to_mongo()
1116+
self.assertTrue(isinstance(mongoed['parent'], ObjectId))
1117+
11081118
def test_objectid_reference_fields(self):
11091119

11101120
class Person(Document):

0 commit comments

Comments
 (0)