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?
Yes , for mongoengine documents use -- 'from rest_framework_mongoengine import serializers' rather from rest_framework import serializers. And the serializer class should inherit DocumentSerializer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With