Skip to content

Conversation

@bagerard
Copy link
Collaborator

@bagerard bagerard commented Sep 16, 2018

The queryset.no_dereference() method was having side effect on the class Field (descriptor) _auto_dereference attribute. Meaning that existing instances could be impacted by a call to no_dereference and suddenly stop dereferencing. See below:

class User(me.Document):
    name = me.StringField()
    
class Organization(me.Document):
    name = me.StringField()
    user = me.ReferenceField(User)

user = User(name='John').save()
Organization(name='GitHub', user=user).save()

org = Organization.objects.first()   # fetch an instance before the no_dereference call
_ = Organization.objects.no_dereference().first() # alters the _auto_dereference of the class field (i.e the ReferenceField descriptor) 
print org.user    # DBRef('user', ObjectId('5b9ec11d4ec5dc58df8290ba')) <-- SHould have been dereferenced

Fixes #1677 (more info in the issue)

Note that:

  • this does not fix the no_dereference context manager
  • CachedReferenceField & GenericReferenceField suffers from the same bug. I'll fix those in a separate PR

@bagerard bagerard force-pushed the no_dereference_queryset_fix_side_effect branch from 2c3938f to f83ae57 Compare September 16, 2018 21:11
@erdenezul erdenezul merged commit 708d1c7 into MongoEngine:master Sep 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no_dereference() disables dereferencing in other documents referencing document

3 participants