MongoEngine / MongoEngine/mongoengine

Provide hook for bidirectional relationship for embedded items?

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

Hi everyone, great work on MongoEngine.

Here's something I have yet to find but really want. The ability to have a function called after the object is created and restored from MongoDB but not yet returned from the query. I think this would be super useful.

Something like:

class Chapter(Document):
    # standard stuff here

   def _loaded(self):
        # this function is called in the situation described 
        # above as part of deserialization
        pass

Here's my use-case. I have two classes:

class Chapter(Document):
    # other fields...
    sections = ListField(EmbeddedDocumentField(Section))
class Section(EmbeddedDocument):
    chapter = None
    # other fields...

I want to associate the section back with it's owning chapter link this:

class Chapter(Document):
    # other fields...
    sections = ListField(EmbeddedDocumentField(Section))

      def _loaded(self):
          for sec in self.sections:
                sec.chapter = self

That way given a chapter I can navigate the sections (already) and given a section, I can get back to the chapter without touching the DB.

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 tracing MongoEngine's deserialization path for Document and EmbeddedDocument objects. Define and test the lifecycle point where the callback runs after restoration but before the query returns, including the Chapter-to-Section back-reference use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.