MongoEngine / MongoEngine/mongoengine
Dereferencing of ReferenceFields do not seem to work
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
Hello,
For a simple scenario like:
Class Post(Document):
title = StringField()
text = StringField()
Class Page(Document):
posts = ListField(ReferenceField(Post))
I was originally thinking that Page.objects.select_related() ought to dereference all the Post objects but it is not working as I was expecting to.
For example this does not have "full" Post objects:
return jsonify([p for p in Page.objects.select_related()])
(I'm running it in Flask, as a response to a Vue application. I would like to respond the full contents of each Post, so I will not have to make further requests later on to populate Post related fields (OK, i'm not using Page/Posts but other entities but the issue remains the same)
Unfortunately, a little loop trick suggested by @JohnAD on #1745 still does not work for me, ending up with an array of "$oid" instead of fully populated Posts objects
page_list = []
for page in Page.objects:
for post in page.posts: # getting the post should pull it (dereference it)
pass
page_list.append(page.to_json())
Is this a regression error (I'm running v0.15.0 with pymongo 3.5.1 on mongo 3.6.3) ?
Thanks..
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example with Page.objects.select_related(), iterating over page.posts, and serializing with page.to_json(). Read the ReferenceField dereferencing and select_related behavior first, then compare the resulting Post objects and serialized output. Done means related Posts are fully populated and included in the serialized response rather than represented only by object IDs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100