Rethink the missing/default field attributes
See original GitHub issueThose attributes come from marshmallow, which is all about serialize/deserialize a document:
missing
is used when the field is not present in the dict to deserializedefault
is used when the field is not present in the document to serialize
However in umongo the focus shifts toward the document itself, so those terms seems a bit clunky
- the
missing
object is already used inside umongo’sDataProxy
to represent field not present in MongoDB. Hence themissing
attribute should mean what value to return if the value ismissing
- the
default
attribute sounds like the value to set by default to the field when creating a new document
In a nutshell the meaning of missing
and default
are reversed between mashmallow and umongo…
What I’m thinking to do:
- Remove the
missing
attribute (in fact just hide it fromabstract.BaseField
constructor and documentation) - Only use the
default
attribute for both missing and default (in marshmallow’s logic) - Add methods to get from the umongo document an equivalent pure mashmallow Schema (see #34 )
The idea is to hide mashmallow logic to expose a more consistent API from the umongo user’s point of view. Then provide a way to get back a pure Marshmallow Schema when needed to do all the custom.
example:
@instance.register
class Person(Document):
name = fields.StrField(default='John Doe')
p = Person()
# Default is set
assert p.name == 'John Doe'
# Default value will be written in database
assert p._data.get('name') == 'John Doe'
# If we want more cunning behavior (e.g. only save in database non-default value)
# we should use a `MethodField` to define custom method for serialization/deseriazation
del p.name
assert p._data.get('name') == missing
# If not present on database, we switch back to default value as well
assert p.name == 'John Doe'
# Now it's customization time !
PersonSchema = p.get_marshmallow_schema()
class MyCustomSchema(PersonSchema):
...
# It could be also useful to provide method to get a specific field
class MyCustomSchema2(marshmallow.Schema)
name = p.get_marshmallow_field('name')
@lafrech What do you think ?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:13 (3 by maintainers)
Top Results From Across the Web
ReQL command: filter - RethinkDB
By default, filter will silently skip documents with missing fields: if the predicate tries to access a field that doesn't exist (for instance, ......
Read more >Rethink Winter 2022 Release Notes - v3.21.8 (Sprint 8) – Buildout ...
Rethink Standard User Profile Now Includes "View All" Permission on Properties and Spaces/Units ... The Rethink Standard User profile, by default, will now ......
Read more >Staff Profile Set Up - Rethink BH Self-Help - Confluence
From Staff Members, Select your staff of choice, and click on General Information from the menu on the left. Click Edit to complete...
Read more >A Practical Introduction to RethinkDB - Pluralsight
This is the default behavior, but if the data is replicated to other nodes within the cluster, we can instruct the query that...
Read more >You're overusing useMemo: Rethinking Hooks memoization
As long as page and type are the same — i.e., no prop changes — resolvedValue will ... Memoizing default state for any...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve been following the discussions about Marshmallow 3 and from what I gathered, the choices made are likely to make our lives easier.
Among the breaking changes, strict would be True by default or even removed and we’d need to catch
ValidationError
, and load would return the deserialized data rather than the (data, errors) tuple. This will break stuff, but ultimately, the code should be easier to read.I think we should keep thing explicit to avoid misleading use of
missing
/default
. For example we only usedefault
un umongo, raise a runtime error ifmissing
is used (to explain how umongo and marshmallow have their behavior changing on this point) and use amarshmallow_missing
/marshmallow_default
fields to pass a custom attribute value when usingas_marshmallow_field
. Of course ifmarshamallow_*
is not provided, we fall back ondefault
value (serializing it on the fly before marshmallow use it)I’ve created a PR which implement this behavior #107, can you have a look and play a bit with it (the subject is pretty complicated, better have two pair of eyes on it !)