MongoEngine / MongoEngine/mongoengine
LazyReferenceField does not return id but a representation the class
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
Hello,
When using a LazyReferenceField (LR), data returned in a retrieve/list call is like that:
<LazyReference(<class 'core.models.Child'>, '5c90074d20c06807b7962cd0')>
So for example we get this:
{
"id": "5c934ec0320ab81cf37a12bb",
"name": "TheParent",
"children": [
{
"child": "<LazyReference(<class 'core.models.Table'>, '5c90074d20c06807b7962cd0')>",
"name": "A child",
"other": "Another field of the child"
}
]
}
I feel like this is not what is expected.
Also I tried to make it return only the id of the child instead of the representation of the LR class by adding a to_representation(self, instance) to the serializer.
Problem is the LR field 'child' is the string representing the class, thus I can't call something like instance.children[0]['child'].id or even a instance.children[0]['child'].fetch() since instance.children[0]['child'] is a simple string representing the LR class.
In the end the simplest solution I found to get the id is by extracting it instance.children[0]['child'][45][69], which is obviously ugly.
Am I missing something?
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 tracing LazyReferenceField serialization and the serializer's to_representation path, then reproduce the retrieve/list response shown in the issue. Determine why the child value becomes a string representation instead of exposing the referenced id; done means the serialized child value has the expected stable representation without extracting characters from the string.
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