i8enn / pydantic-odm

Small async ODM for MongoDB based in Motor and Pydantic library
MIT License
39 stars 8 forks source link

Create empty `id` field in models #23

Open i8enn opened 4 years ago

i8enn commented 4 years ago

I noticed that saving the models also creates an empty id in both the _doc attribute for DBPydanticMixin instance and the MongoDB collection. Also, if you call the dict() method on the model, this field will be included in the result. This is especially noticeable with nested models. But just comparing 2 models - everything will be fine.

This is not entirely good.

Mini-prof from test breakpoint ```python (ipdb>) debug(post, post.dict()) :1 (no code context for debug call, code inspection impossible) Post( title='test', body='test_body', author=User( username='test', created=datetime.datetime(2020, 7, 21, 16, 16, 43, 725327), age=10, type=, ), comments=None, _id=ObjectId('5f16dcabbee4a5bab3584a8f'), _doc={ '_id': ObjectId('5f16dcabbee4a5bab3584a8f'), 'title': 'test', 'body': 'test_body', 'author': { 'username': 'test', 'created': datetime.datetime(2020, 7, 21, 16, 16, 43, 725327), 'age': 10, 'type': , 'id': None, }, 'comments': None, }, ) (Post) { 'title': 'test', 'body': 'test_body', 'author': { 'username': 'test', 'created': datetime.datetime(2020, 7, 21, 16, 16, 43, 725327), 'age': 10, 'type': , 'id': None, }, 'comments': None, 'id': ObjectId('5f16dcabbee4a5bab3584a8f'), } (dict) len=5 (ipdb>) ```

Environment:

Python - 3.8 motor - 2.1.0 pymongo - 3.10.1 pydantic - 1.6.1 pydantic-odm - 0.2

i8enn commented 4 years ago

This issue don't resolved. Need checking all methods of creating or updating models.