MongoEngine / MongoEngine/mongoengine
(auto) De-referencing not working as expected using select_related
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
I have two models that are related. User, and Role
class Role(Document):
name = StringField(required=True)
project = ReferenceField(Project, reverse_delete_rule=CASCADE)
description = StringField(required=True)
permissions = ListField(EmbeddedDocumentField(Permission))
class User(Document):
full_name = StringField(required=True)
username = EmailField(required=True)
password_hash = StringField(required=True)
avatar = StringField()
date_created = DateTimeField(default=datetime.datetime.utcnow)
project = ReferenceField(Project, reverse_delete_rule=CASCADE)
role = ReferenceField(Role, reverse_delete_rule=DENY)
I can't seem to figure out how to obtain a User document that has the role and project fields dereferenced. My initial solution was to call select_related() on the QuerySet containing a selection of users.
users = User.objects(project=project_id).select_related()
This has no effect. type(users[0].role) == ReferenceField, and not Role. I read in the documentation that ReferenceFields should be dereferenced automatically by default (because _auto_dereference is set to True on al fields. When trying to access this property, however, I get an error stating that this property does not exist on role field.
Curiously, somewhere else in my application, select_related works just as expected on document that has a ListField(ReferenceField(...)).
mongoengine version is 0.16.2
Does anyone see what I'm doing wrong?
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 with the User.role and User.project ReferenceField definitions and the shown User.objects(project=project_id).select_related() query. Compare scalar reference behavior with the ListField(ReferenceField(...)) case described in the issue. Done means selecting users dereferences the role and project fields as expected, while preserving the working list-reference behavior.
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
- 42/100