0

I have a list field in my django model for users as follows: name = models.CharField() email = models.CharField() profiles = ListField()

I am using mongo as storage database with help of django_mongodb_engine. For one user, model object could be like

 {name: 'Alice', email: 'abc', profiles: ['Read']} 
For another user it could be:
 {name: 'John', email: 'xyz', profiles: ['Read', 'Write']} 

I want to search for users which are assigned Read profile to them. In mongo db shell it can be performed simply by adding in query

{'profiles': 'Read'} 
. Can we perform search like that via django querysets ? When I tried, it gave me an exception "list indices must be integers, not str". Please guide me in this regards. Thanks
Dania
  • 1,007
  • 1
  • 14
  • 21

1 Answers1

1

One possible solution may be using raw queries with help of MongoDBManager:

from django_mongodb_engine.contrib import MongoDBManager

class UserModel(models.Model):
    name = models.CharField()
    email = models.CharField()
    profiles = ListField()

    objects = MongoDBManager()

and then:

UserModel.objects.raw_query({'profiles': 'Read'})
Siegmeyer
  • 4,312
  • 6
  • 26
  • 43
  • I have edited my question, please have a look again to understand problem better. Thanks. – Dania Aug 24 '16 at 11:00