Giter Site home page Giter Site logo

Comments (17)

rozza avatar rozza commented on May 19, 2024 2

Added in 7d90aa7 - uses _instance

from mongoengine.

rozza avatar rozza commented on May 19, 2024

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.

sergio-bershadsky avatar sergio-bershadsky commented on May 19, 2024

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.

sergio-bershadsky avatar sergio-bershadsky commented on May 19, 2024

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.

sergio-bershadsky avatar sergio-bershadsky commented on May 19, 2024

Wonderful!

from mongoengine.

sergio-bershadsky avatar sergio-bershadsky commented on May 19, 2024

Seems to be deprecated, this ability disapeared in last commits

from mongoengine.

cburmeister avatar cburmeister commented on May 19, 2024

@rozza @nikitinsm any information on this??

from mongoengine.

ochen avatar ochen commented on May 19, 2024

@rozza

any reason why this pointer is gone?

from mongoengine.

lig avatar lig commented on May 19, 2024

regression?

from mongoengine.

vartagg avatar vartagg commented on May 19, 2024

I didn't know about this feature. Is it really not working? mongoengine==0.8.7 all still working.

from mongoengine.

lafrech avatar lafrech commented on May 19, 2024

Fixed in #1131?

from mongoengine.

touilleMan avatar touilleMan commented on May 19, 2024

@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.

lafrech avatar lafrech commented on May 19, 2024

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.

touilleMan avatar touilleMan commented on May 19, 2024

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.

thedrow avatar thedrow commented on May 19, 2024

I'd be happy to welcome you on board.

from mongoengine.

thedrow avatar thedrow commented on May 19, 2024

@lafrech Done. Check your email.

from mongoengine.

lafrech avatar lafrech commented on May 19, 2024

Thanks. Invitation accepted. I can now close this bug myself as the issue should be fixed.

from mongoengine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.