djangomongodbpymongodjango-ormdjango-nonrel

How to query a field in an ListField of EmbeddedModelField in django-nonrel?


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/django@nonrel-1.3
git+https://github.com/django-nonrel/djangotoolbox@toolbox-1.3
git+https://github.com/django-nonrel/mongodb-engine@mongodb-engine-1.3

Solution

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