MongoEngine / MongoEngine/mongoengine

Fill _instance when adding an embedded document to a list

Open
#1,925 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

EmbeddedDocument._instance is not filled when the embedded document as added to a document's list of embedded docs:
https://github.com/MongoEngine/mongoengine/commit/7d90aa76ff7116269dea42f2c6629ea6b868b0de#diff-da780e321f79f4602bab26240c7b0d7dR87

ipdb> l
    136         benefit = PurchaseBenefit()
    137         coupon.benefits.append(benefit)
    138         # HACK: force MongoEngine to fill _instance
    139         import ipdb; ipdb.set_trace()
--> 140         coupon.benefits[0]
    141     form = PurchaseBenefitForm(request.form if request.method == 'POST' else None, obj=benefit)
    142     errors = []
    143     if request.method == 'POST' and form.validate():

ipdb> isinstance(benefit, mongoengine.EmbeddedDocument)
True
ipdb> benefit._instance
ipdb> n
> /Users/Vic/projects/brain2/brain/crm/coupons.py(141)edit_coupon_benefit()
    140         coupon.benefits[0]
--> 141     form = PurchaseBenefitForm(request.form if request.method == 'POST' else None, obj=benefit)
    142     errors = []

ipdb> benefit._instance
Coupon(id='5b915f6a2ac6b371ed8f21df', ...)
ipdb>

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 with the referenced commit, especially the linked diff around line 87, and reproduce the append sequence shown in the issue. Check the EmbeddedDocument behavior before and after adding it to the parent list; done means _instance is populated immediately after append, with a regression test covering that case.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.