Refty / mongo-thingy

:leaves: Powerful schema-less ODM for MongoDB and Python (sync + async)
https://mongo-thingy.readthedocs.io
MIT License
68 stars 12 forks source link

Convert ObjectId to string id for all the view #32

Closed shunyeka closed 4 years ago

shunyeka commented 4 years ago

I am trying to convert ObjectID to string id for viewing because unable to convert ObjectID to json.

Here's what I am doing.

class Test(Thingy):
    @property
    def id(self):
        return str(self._id)

Test.add_view(name="strid", defaults=True, exclude="_id", include="id")

This is saving id as None in mongodb. What is the issue here?

ramnes commented 4 years ago

Hey @shunyeka,

No id field should be inserted if you do not set it on your instance. Can you provide a more complete example so that I can try to reproduce?

On a side note, I would recommend you to modify your JSON encoder / serializer to support the ObjectId type, rather than implementing a property for every ObjectId field in every collection. See issue #31 for examples with Flask-JSON; the same principle applies whatever the library.

ramnes commented 4 years ago

I'll go ahead and close this, but feel free to answer here and I'll reopen if needed. Thanks!