MongoEngine / MongoEngine/mongoengine

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

Open
#2,494 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

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'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read fields.py, base/fields.py, and dereference.py around the stack frames, then reproduce the failure with the Gig, Image, and Video models shown. Add a regression test for a populated ListField containing GenericLazyReferenceField values; done means accessing g.media after retrieval no longer raises AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.