1

Let's say I have this:

class Parent(models.Model):
    id = models.IntegerField(primary_key=True)
    children = ListField(EmbeddedModelField('Child'))

class Child(models.Model):
    id = models.IntegerField(primary_key=True)

In the mongo interactive shell, finding Parent's with a particular Child is as easy as:

db.myapp_parent.find({'children.id': 123})

How is this done in django-nonrel?

I tried a few things including I looked for raw queries but raw_results is not a method in Parent.objects for some reason.

FWIW, this is what I have in my requirements.txt:

git+https://github.com/django-nonrel/[email protected]
git+https://github.com/django-nonrel/[email protected]
git+https://github.com/django-nonrel/[email protected]

2 Answers 2

5

I think I found the answer myself:

https://groups.google.com/forum/#!topic/django-non-relational/kCLOcI7nHS0

Basically, looks like this is not supported yet.

So the workaround is raw queries.

In order to make raw queries the code in the question should be modified to:

from django_mongodb_engine.contrib import MongoDBManager


class Parent(models.Model):
    id = models.IntegerField(primary_key=True)
    children = ListField(EmbeddedModelField('Child'))

    objects = MongoDBManager()


class Child(models.Model):
    id = models.IntegerField(primary_key=True)

Then

Parent.objects.raw_query({'children.id': 123})

works.

1

Looked around for a while and suddenly the following mentioned there worked like magic for me, that appears to avoid the need for raw queries (adapted to your example):

from django_mongodb_engine.query import A

...

Parent.objects.filter( children = A('id', '123') )

As for requirements:

git+https://github.com/django-nonrel/[email protected]
git+https://github.com/django-nonrel/[email protected]
git+https://github.com/django-nonrel/mongodb-engine
#(django-mongodb-engine==0.6.0)
#(pymongo==3.2)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.