MongoEngine / MongoEngine/mongoengine

Enable subclassing of EmbeddedDocumentList

Open
#2,064 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

While trying to achieve a more native/OOP like use of the models in my code, I encountered the following issue:
I have the following structure:

  • class P(Document)
  • class M(EmbeddedDocument)

P contains a list of Ms, hence in the definition of P I have:
ms = EmbeddedDocumentListField(M)
The problem is that I want to achieve the following usage:

p = P()
p.ms.my_creation_func()

To enable that, I need the object that returns from p.ms to have my_creation_func. Since ms is defined with a descriptor, that receives only the class that inherits from EmbeddedDocument, I have no way of achieving that properly.

The solution I have for now, is to subclass the whole triplet (EmbeddedDocument, EmbeddedDocumentList, EmbeddedDocumentListField), and allow the subclass of EmbeddedDocumentListField to accept the class of the list it wraps as a constructor parameter.
To wrap it up, I need to override the __get__ function of the EmbeddedDocumentListField's subclass. by combining all of the __get__ functions that appear throughout the inheritance tree, and modify them to work with the list class I provided as a parameter, instead of working with EmbeddedDocumentList.

A good solution would allow the EmbeddedDocumentListField c'tor to accept the list class as a parameter, put it in some field, and the __get__ functions in the super classes to work with that field.

If there's any other solution, which I can apply locally, I'd be glad to know.

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 EmbeddedDocumentListField and its get implementations, along with EmbeddedDocumentList and EmbeddedDocument. Reproduce the P, M, and ms = EmbeddedDocumentListField(M) example, then determine whether accepting a custom list class preserves the existing descriptor behavior. Done means a subclass can expose its own method through p.ms without requiring a separate reimplementation of the descriptor hierarchy.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.