Skip to content

Commit 23ba186

Browse files
authored
Merge 5a03f5a into 370460d
2 parents 370460d + 5a03f5a commit 23ba186

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

mongoengine/dereference.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import partial
2+
13
from bson import SON, DBRef
24

35
from mongoengine.base import (
@@ -174,7 +176,10 @@ def _fetch_objects(self, doc_type=None):
174176
refs = [
175177
dbref for dbref in dbrefs if (col_name, dbref) not in object_map
176178
]
177-
references = collection.objects.in_bulk(refs)
179+
if isinstance(collection.objects, partial):
180+
references = collection.objects().in_bulk(refs)
181+
else:
182+
references = collection.objects.in_bulk(refs)
178183
for key, doc in references.items():
179184
object_map[(col_name, key)] = doc
180185
else: # Generic reference: use the refs data to convert to document

tests/queryset/test_queryset.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,6 +4016,28 @@ def objects(klass, queryset):
40164016
assert 0 == Foo.objects.count()
40174017
assert 1 == Bar.objects.count()
40184018

4019+
def test_select_related_when_referenced_has_custom_queryset_manager(self):
4020+
class Foo(Document):
4021+
@queryset_manager
4022+
def objects(klass, queryset, arg1=None, arg2=None, **kwargs):
4023+
return queryset(**kwargs)
4024+
4025+
class Bar(Document):
4026+
foo = ReferenceField(Foo)
4027+
4028+
Foo.drop_collection()
4029+
Bar.drop_collection()
4030+
4031+
foo = Foo()
4032+
foo.save()
4033+
4034+
Bar(foo=foo).save()
4035+
4036+
Bar.objects().select_related()
4037+
4038+
assert 1 == Foo.objects().count()
4039+
assert 1 == Bar.objects().count()
4040+
40194041
def test_query_value_conversion(self):
40204042
"""Ensure that query values are properly converted when necessary."""
40214043

0 commit comments

Comments
 (0)