MongoEngine/mongoengine

AttributeError: 'GenericLazyReferenceField' object has no attribute '_get_db'

Opened this issue · 3 comments

Upon attempting to access a ListField wrapping a GenericLazyReferenceField the above internal error is thrown.
Here are the relevant portions of my use case:

class Image(db.Document):
    __api__ = "img"
    data = db.ImageField()

class Video(db.Document):
    __api__ = "video"
    data = db.FileField()

class Gig(db.Document):
    media = db.ListField(db.GenericLazyReferenceField()

Media is a list of lazy references to Image and Video Documents. While I have no issue writing to Gig's media field, I am unable to access the field after retrieving a Gig object from the database. This issue only happens when there is at least one value within the media field.

Here is the call stack I get when attempting to access the field:

>>> g = Gig.objects(name__icontains='test').first()
>>> g.media
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/---/Library/Python/3.8/lib/python/site-packages/mongoengine/fields.py", line 930, in __get__
    return super().__get__(instance, owner)
  File "/Users/---/Library/Python/3.8/lib/python/site-packages/mongoengine/base/fields.py", line 299, in __get__
    instance._data[self.name] = _dereference(
  File "/Users/---/Library/Python/3.8/lib/python/site-packages/mongoengine/dereference.py", line 97, in __call__
    self.object_map = self._fetch_objects(doc_type=doc_type)
  File "/Users/---/Library/Python/3.8/lib/python/site-packages/mongoengine/dereference.py", line 186, in _fetch_objects
    references = doc_type._get_db()[collection].find(
AttributeError: 'GenericLazyReferenceField' object has no attribute '_get_db'

Anyone solved this? We've encountered this as well.

Okay we found the issue. The list was populated with DBRefs instead of the GenericLazyReferenceField objects. These came from .no_deference() query, which suggests that the pointers returned across different operations are inconsistent.

It's been really sad using this library :( Absolute joy on the first day. Absolute terror on the second week.

I'm running into a similar issue with GenericReferenceField.