Reputation: 3035
I am using the Django Rest Framework with a MongoDB and I am trying to retrieve a document from the MongoDB and then serialize it to json in order to send it back in response to a request.
I have a database model that looks like this:
class TTest(Document):
user_id = StringField()
submission_status = StringField()
and the corresponding serializer:
class TTestSerializer(serializers.ModelSerializer):
class Meta:
model = models.TTest
pk = "_id"
Now having these, I wish to retrieve a document from the DB and I tried to follow exactly the tutorial regarding serialization using Django Rest Framework:
queryset = models.TTest.objects.filter(user_id='bbb') # retrieving documents using mongoengine
serializer = TTestSerializer(queryset)
dt = serializer.data
print "DATA SERIALIZED: ", dt
When I am running these lines, trying to serialize, I get back this error:
AttributeError: 'MetaDict' object has no attribute 'pk'
Theoretically I think it should work, since I declared in the serializer's metadata that the primary key is "_id"...Can anyone help me with an advice?
Thanks
Upvotes: 2
Views: 3819
Reputation: 18111
Mongoengine is not a direct replacement of Django's ORM and as such may not natively integrate with libraries expecting the normal ORM.
You might want to try http://django-tastypie-mongoengine.readthedocs.org/en/latest/ that has been made to work with mongoengine.
Upvotes: 0
Reputation: 33901
There's no such option 'pk' in Serializer classes, so that won't have any affect.
I don't know much about mongoengine, but I expect you need to be using plain 'Serializer' classes rather than 'ModelSerializer' if the object isn't a standard Django model instance.
It'd be worth asking questions about REST framework and mongoengine on the REST framework mailing list, since I know there's some other folks who've been doing the same.
https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework
Upvotes: 4