Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find documents of class and subclasses #1924

Closed
tulaeff opened this issue Oct 17, 2018 · 9 comments
Closed

Find documents of class and subclasses #1924

tulaeff opened this issue Oct 17, 2018 · 9 comments

Comments

@tulaeff
Copy link

tulaeff commented Oct 17, 2018

Please, help me.

from mongoengine import *

# ... my params for MongoDB connection

class Subject(DynamicDocument):
    meta = { 'collection': 'subjects', 'allow_inheritance': True }
    type = StringField(default='foobar')

class FooSubject(Subject):
    type = StringField(default='foo')

class BarSubject(Subject):
    type = StringField(default='bar')

when I write this:

subjects = Subject.objects()

it returns documents where '_cls' == 'Subject' only. And how I can get all documents from collection subjects?

P.S. Sorry for my English

@stefanvanburen
Copy link
Contributor

this might just be a transcription error, but presumably you want:

class FooSubject(Subject):

rather than:

class FooSubject(subject):

@mjmonroe
Copy link

mjmonroe commented Oct 17, 2018

Have you taken a look at this post? #450 (comment)
In your case it would be,
Subject.objects(_cls='Subject.FooSubject')

Another way would be to pass a raw query to MongoEngine.
Subject.objects(__raw__={{ "_cls": "/^Subject.*/i" }})

@tulaeff
Copy link
Author

tulaeff commented Oct 17, 2018

this might just be a transcription error, but presumably you want:

class FooSubject(Subject):

rather than:

class FooSubject(subject):

Yes, of course, thanks. I corrected my question. Thank you very much!

@bagerard
Copy link
Collaborator

Querying the parent class should return you instances of the class + its subclasses by default and this seems to work. Do you observe something different?

image

@tulaeff
Copy link
Author

tulaeff commented Oct 17, 2018

Subject.objects(raw={{ "_cls": "/^Subject.*/i" }})

Traceback (most recent call last):
  File "test.py", line 20, in <module>
    subjects = Subject.objects(__raw__={{ "_cls": "/^Subject.*/i" }})
TypeError: unhashable type: 'dict'

@tulaeff
Copy link
Author

tulaeff commented Oct 17, 2018

Querying the parent class should return you instances of the class + its subclasses by default and this seems to work. Do you observe something different?

image

I thought so too. This is the expected behavior. But no

@bagerard
Copy link
Collaborator

The code I've posted shows that it works fine (on latest version). I'm able to retrieve FooSubject through Subject.objects(). In case you observe something different, please post a minimal reproducible code snippet that shows the issue. It could also be due to the existing data in your mongodb. I see that you are using DynamicDocument (instead of Document) so I guess you have some uncommon data in your collection.

@tulaeff
Copy link
Author

tulaeff commented Oct 18, 2018

The code I've posted shows that it works fine (on latest version). I'm able to retrieve FooSubject through Subject.objects(). In case you observe something different, please post a minimal reproducible code snippet that shows the issue. It could also be due to the existing data in your mongodb. I see that you are using DynamicDocument (instead of Document) so I guess you have some uncommon data in your collection.

I realized. It works when I import in my test.php script ALL child classes (not Subject only). I have them in different files. Thanks for your answers.

@bagerard
Copy link
Collaborator

bagerard commented Oct 18, 2018

Happy to help :). Please mark the ticket as "closed" in case the issue is resolved

@tulaeff tulaeff closed this as completed Oct 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants