I am using Mongodb with mongoengine as the backend for the API in Django. The structure I use to create the api is the Django Rest Framework.
I need to save the dictionary in a field in Mongo, and the best I did when the post method is called is to use charfield and parse the dictionary in restore_object function.
Is there a better way to achieve this?
Better create a dict field? I donβt know how hard it is.
Thanks.
edited to show some code, note that I store the dictionary as a dict (DictField), and the contents may change from one object to another.
my mongoengine model looks something like this:
class MyDoc(mongoengine.Document): name = mongoengine.StringField(max_length=200) context = mongoengine.DictField()
and my serializer is something like:
class MyDocSerializer(serializers.Serializer): name = serializers.CharField(max_length=200) context = serializers.CharField() url = serializers.HyperlinkedIdentityField( view_name="drf:mydoc-detail",) def __init__(self,*args,**kwargs): super(MyDocSerializer,self).__init__(*args,**kwargs) def restore_object(self, attrs, instance=None):
source share