Skip to content

Add possibility to update a document or a whole query from JSON #323

@cod3monk

Description

@cod3monk

We already have to_json() and from_json(data), but the later only creates a whole new Document. My problem is, that I need to update only certain fields and the document is already loaded.

It would be nice to be able to load a partial json representation of a Document and only update the passed fields.

Example:

>>> class Test(Document):
...     name = StringField()
...     foo = StringField(default="bar")
...
>>> t = Test()
>>> t.save()
<Test: Test object>
>>> t.to_json()
'{"_id": {"$oid": "5198cf4759055e1762366812"}, "foo": "bar"}'
>>> t.from_json('{"name": "Baz!"}', update_inplace=True)
<Test: Test object>
>>> t.name
u'Baz!'

Here from_json with update_inplace=True would return self and not a new Test object.

Alternatively an update_from_json function could do the same.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions