MongoEngine / MongoEngine/mongoengine

Cannot use to_json in pre_init

Open
#2,173 0 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

  File ".../venv/lib/python3.7/site-packages/mongoengine/base/document.py", line 423, in to_json
    return json_util.dumps(self.to_mongo(use_db_field), *args, **kwargs)
  File ".../venv/lib/python3.7/site-packages/mongoengine/document.py", line 252, in to_mongo
    data = super(Document, self).to_mongo(*args, **kwargs)
  File ".../venv/lib/python3.7/site-packages/mongoengine/base/document.py", line 328, in to_mongo
    value = self._data.get(field_name, None)
AttributeError: _data

signals.pre_init.connect(pre_init)

pre_init has document as second argument. The the pre_init function is invoked, document is empty. to_json should return an empty json object.

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

Start by reproducing the pre_init failure and read the referenced paths in mongoengine/base/document.py around lines 328 and 423, plus document.py around line 252. Trace how the empty document reaches to_json, then add coverage for the reported case; done means calling to_json in pre_init returns an empty JSON object without raising 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.