Comments (17)
Added in 7d90aa7 - uses _instance
from mongoengine.
Can you please give an example of why you need to do this via the embedded document (bottom up) rather than from the document (top down) ?
from mongoengine.
I've faced the shortage of this feature several times in my new project, a have a very dirty implemention for that case, but I think it is general problem, for example I add some data in child document, and I need to recalс cached data in parent document
class ListChild(EmbeddedDocument)
pass
class Child(EmbeddedDocument):
items = List(EmbeddedDocumentField(ListChild))
def add(item):
self.items.append(item)
self.get_parent().total = len(self.items) #self.get_parent() returns a reference to Parent instance
class Parent(Document):
child = EmbeddedDocumentField()
total = IntegerField()
p = Parent.objects.first()
p.child.add(ListChild())
p.save()
#effected: p.child.items and p.quantity
from mongoengine.
What i'm doing for this case, it doesn't tested proper use it for your own risc. It surtanly doesn't works with Lists and Dicts in run time and with Dicts at init.
class MyEmbeddedDocument(EmbeddedDocument):
_root_doc = None
@property
def root_doc(self):
"""
returns the root document instance
"""
return self._root_doc
class MyDocument(Document)
def __init__(self, **kwargs):
super(MDocument, self).__init__(**kwargs)
#set back reference to main document (root document)
self._messages = list()
for embedded_doc in self.get_all_embedded_docs():
embedded_doc._root_doc = self
def __setattr__(self, key, value):
"""
Connects root document in run time, doesn't work with iterables
@example:
doc = SomeDocument()
doc.embedded_document = SomeEmbeddedDoceumnt()
doc.embedded_document.root_doc == doc #True
#or even
doc = SomeDocument()
doc.embedded_document.sub_embedded_document = SomeEmbeddedDoceumnt()
doc.embedded_document.sub_embedded_document.root_doc == doc #True
"""
if issubclass(type(value), MyEmbeddedDocument):
root_doc = getattr(value, 'root_doc', None)
if root_doc is None and issubclass(type(self), MyDocument):
value._root_doc = self
object.__setattr__(self, key, value)
def get_all_embedded_docs(self):
"""collects all embedded documents for the document"""
embedded_docs = list()
def check_field(value):
if type(value) in (BaseList, list):
check_list(value)
if issubclass(type(value), MyEmbeddedDocument):
embedded_docs.append(value)
check_doc(value)
def check_doc(doc):
for name, value in doc._data.iteritems():
check_field(value)
def check_list(list):
for value in list:
check_field(value)
check_doc(self)
return embedded_docs
from mongoengine.
Wonderful!
from mongoengine.
Seems to be deprecated, this ability disapeared in last commits
from mongoengine.
@rozza @nikitinsm any information on this??
from mongoengine.
any reason why this pointer is gone?
from mongoengine.
regression?
from mongoengine.
I didn't know about this feature. Is it really not working? mongoengine==0.8.7 all still working.
from mongoengine.
Fixed in #1131?
from mongoengine.
@lafrech Maybe this old ticket hasn't been closed... Have you experieced the bug (given #1131 has been integrated in 0.10.1) ?
from mongoengine.
Hi @touilleMan.
Maybe this old ticket hasn't been closed...
Yes, pretty sure.
Have you experieced the bug (given #1131 has been integrated in 0.10.1) ?
No. I just found this page while searching for such a feature. Thanks to the link to #1131 I knew that the feature was supposed to be working correctly and I'm now using it.
My point was to ping the OP to let him close the bug. You should probably close it, since you have write access. He can reopen if issues arise.
More generally, there is currently 260 open bugs and I'm pretty sure a lot could be closed as fixed, obsolete, lacking info, or at least tagged as questions. IMHO, this would be better for the project both in terms of organization (identify actual issues) and image (a lot of open / unanswered bugs may let people think the project lacks maintenance; TBH I was a bit reluctant to use Mongoengine because or this). I offered to help cleaning flask-mongoengine bugtracker as a contribution to the project. I could give a hand on mongoengine as well. We may need to define a workflow (which labels to use, for instance), or maybe this has already been defined and it could be useful to add it to CONTRIBUTING.rst. If there is a better place to discuss this, please tell me.
from mongoengine.
You're right about all those open issues.
Beside we're a small team (currently only @thedrow and me have the time to work on), so I think an offered hand is something really valuable for the sake of the project !
@thedrow what do you think about adding @lafrech to the project ?
PS: Nice to see another bordelais here ;-)
from mongoengine.
I'd be happy to welcome you on board.
from mongoengine.
@lafrech Done. Check your email.
from mongoengine.
Thanks. Invitation accepted. I can now close this bug myself as the issue should be fixed.
from mongoengine.
Related Issues (20)
- how to switch collection when query document HOT 1
- Typing support HOT 1
- InvalidDocument when using EmbeddedDocument primary key and ReferenceField
- The method document.save() will unseted fields that equal default
- Getting the following error , ValidationError(message, errors=errors) mongoengine.errors.ValidationError: ValidationError (User:656126eff188fc38533e1a9f) (You can only reference documents once they have been saved to the database: ['partners']) HOT 2
- Sorting on nested field that contains `__` in its name. HOT 1
- Validation doesn't work on embedded documents with inheritance HOT 1
- using vector search with mongoengine HOT 2
- missing link in documentation
- Document not working well with Fastapi & Pydantic
- Updating an inner dict within a `DictField` of a `DynamicDocument` raise an `AttributeError`
- Request the next release of 0.27.0 HOT 2
- “AttributeError: ‘_thread._local’ object has no attribute ‘no_dereferencing_class’ in Multithreaded Environment” HOT 11
- Exception raised: 2 or more items returned, instead of 1 HOT 3
- ImportError: cannot import name '_CaseInsensitiveDictionary' from 'pymongo.common' HOT 3
- `choices` not validated after update
- Support PEP484
- Changes from clear() on ListField are not saved
- visualization tools for the models
- `Queryset.modify` not support kwarg `array_filters` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoengine.