Reputation: 4773
I am using Pymodm as a mongoDB odm with python flask. I have looked through code and documentation (https://github.com/mongodb/pymodm and http://pymodm.readthedocs.io/en/latest) but could not find what I was looking for.
I am looking for an easy way to fetch data from the database without converting it to a pymodm object but as plain JSON. Is this possible with pymodm?
Currently, I am overloading the flask JSONEncoder to handle DateTime and ObjectID and use that to convert the pymodm Object to JSON.
Upvotes: 2
Views: 1955
Reputation: 553
If you need to build CRUD api you might also want to check this little package, basically DRF for pymodm
So if you want to create CREATE/UPDATE/DELETE it would look like this
from api.pymodm_rest import viewsets
class ServiceAreaViewSet(viewsets.ModelViewSet):
queryset = ServiceArea.objects
instance_class = ServiceArea
lookup_field = '_id'
[https://github.com/lokoArt/pymodm_rest][1]
Upvotes: -1
Reputation: 9246
Having:
from pymodm import MongoModel, fields
import json
class Foo(MongoModel):
name = fields.CharField(required=True)
a=Foo()
You can do:
jsonFooString=json.dumps(a.to_son().to_dict())
Upvotes: 1
Reputation: 1104
It is not obvious from the PyMODM documentation, but here's how to do it:
pymodm_obj.to_son().to_dict()
Actually, I just re-read your question, and I don't think anything is forcing you to use PyMODM everywhere in your project once you have made the decision to use it. So if you are just looking for the JSON structures, you could just use the base pymongo package functionality.
Upvotes: 2