Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django serialization to JSON error: 'MetaDict' object has no attribute 'concrete_model'

I am working on an application that uses Django and MongoDB (as a model). I am trying to initialize (by hand) a model object (in order to send it further to another server), using the data got from a form. The model looks like this:

class MyModel(DynamicDocument):
    study_name = StringField(default="first study")
    individual_name = StringField(default="individual")
    file_list = ListField(StringField)   # this is a list of paths to some files

In the form I am doing the following:

pilot_object = MyModel()
pilot_object.individual_name = self.data['individual_name']
pilot_object.study_name = self.data['study_name']
pilot_object.file_list = files_list #file paths (strings)

where self.data is the data received from the form. Now I want to serialize to JSON this object (pilot_object) as described in the documentation:

data_serialized = serializers.serialize('json', [pilot_object, ])

but I get this error:

'MetaDict' object has no attribute 'concrete_model'

and the serialization fails.

Can anyone help?

like image 439
Clara Avatar asked Jan 23 '13 15:01

Clara


1 Answers

Yes , for mongoengine documents use -- 'from rest_framework_mongoengine import serializers' rather from rest_framework import serializers. And the serializer class should inherit DocumentSerializer.

like image 61
Vaishali Chuphal Avatar answered Oct 05 '22 17:10

Vaishali Chuphal