'UserManager' object has no attribute 'raw_query'

441 Views Asked by At

nonrel(Mongodb) with tastypie.I have created one resource class which return selected auth_user from the database.Because of some reasons I did change _id field type from ObjectId to Int64.I used raw_query() for accessing desired auth_user but it doesn't seems to work.Can anyone suggest me what should I do to make raw_query() work for UserModel.

Here is my tastypie resource:

class UserResource(ModelResource):
    class Meta:

        queryset=User.objects.raw_query({'_id':5})
        resource_name="user"
        fields=["username","first_name","last_name"]
        include_resource_uri=False
        allowed_methods=["get"]

output:

'UserManager' object has no attribute 'raw_query' 
2

There are 2 best solutions below

3
dan-klasson On

This is not related to Tastypie at all. Are you sure you need a raw query, or is the above just an example?

By looking at the docs you should be able to do the following:

from django_mongodb_engine.contrib import MongoDBManager

class User(models.Model):
    mongo_manager = MongoDBManager()

Then you should be able to use it as you do above, i.e:

queryset=User.mongo_manager.raw_query({'_id':5})
0
riya ahuja On

I myself workaround to this problem and for the sake of others help I am answering my own question.Hope this will help someone.

This solution is to create a proxy model.By declaring proxy models we can do opertions on parent model and even can modify manager.

proxy model:

class UserClass(User):
    objects = MongoDBManager()
    class Meta:
        proxy=True

Resource:
class UserResource(ModelResource): class Meta:

        queryset=UserClass.objects.raw_query({'_id':5})
        resource_name="user"
        fields=["username","first_name","last_name"]
        include_resource_uri=False
        allowed_methods=["get"]