I’m trying to select a number of attributes from an object. The doc says that I should use only.
I have UserObject like this:
from mongoengine.django.auth import User
class MyUser(User):
username = StringField()
firstname = StringField()
...
And a query like this:
user = MyUser.objects.get(username='Katyss').only('firstname')
But I get something like this:
MyUser object has no attribute ‘only’
Any ideas?
Edit:
Regarding to Daniel Roseman‘s post I tried to use only with filter. But I get the following error message:
error_message”: “coercing to Unicode: need string or buffer, NoneType found”
Google’ing this error I found a guy who claims that using the unicode()-function in the model/documents would help. But I want this only-selection just this one time – not every time. Any ideas?
onlyis a QuerySet method. It’s for returning a list of instances with only those attributes selected. You’ve usedget, which returns an instance, and the instance doesn’t have that method.I’m sure however that you don’t actually need it. Unless you have hundreds of attributes on your MyUser model, it’s not significantly more inefficient to just get the whole instance and use what you need.