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
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'})