pydantic-odm icon indicating copy to clipboard operation
pydantic-odm copied to clipboard

Create empty `id` field in models

Open i8enn opened this issue 4 years ago • 1 comments

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
(ipdb>) debug(post, post.dict())                                                                                                                                                                                                             
<stdin>:1 <module> (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=<UserTypesEnum.Reader: 'reader'>,
        ),
        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': <UserTypesEnum.Reader: 'reader'>,
                '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': <UserTypesEnum.Reader: 'reader'>,
            '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 avatar Jul 21 '20 12:07 i8enn

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

i8enn avatar Jul 24 '20 03:07 i8enn