-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NoneType for (de)serializing None options for other schematypes #121
Conversation
Can you update the sphinx docs with more information about this new feature? |
I have added some documentation. |
@mrijken, you've just saved me a couple of hours of having to implement this myself. |
Any updates on this pull request? I'm looking into migrating one of our projects to use colander 1.0b1, but I need some integers to have an optional None value. I'd rather update the schemas than update all the views. |
without converting `colander.null` to None outside the schema. In other words, you have defined in the | ||
schema that the node can have None as value. | ||
|
||
Note: NoneType can be used for strings, because the machinery does not known whether a serialization of `""` is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should that read "cannot be used for strings"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I should read it twice before committing.
2013/10/16 Tres Seaver notifications@github.com
In docs/null.rst:
- assert deserialized['age'] == None
- deserialized = schema.deserialize({'name':'Fred', 'age': '19'})
- assert deserialized['age'] == 19
- serialized = schema.serialize({'name':'Fred', 'age': None})
- assert serialized['age'] == ''
- serialized = schema.serialize({'name':'Fred', 'age': 19})
- assert serialized['age'] == '19'
+When you use
colander.NoneType
you can pass the results of the deserialization directly to a database,
+without convertingcolander.null
to None outside the schema. In other words, you have defined in the
+schema that the node can have None as value.
+
+Note: NoneType can be used for strings, because the machinery does not known whether a serialization of""
isShould that read "cannot be used for strings"?
—
Reply to this email directly or view it on GitHubhttps://github.com//pull/121/files#r7001230
.
Any news about this pull request? This seems very useful for me… |
The None Value | ||
============== | ||
|
||
Every colander SchemaType will only (de)serialize a value which belongs to its type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason SchemaType is not actually a public API, so it probably shouldn't be mentioned here.
Basically every type is supposed to handle |
Thank you for the patch. Can you please:
|
updated in #258 |
In my apps, I have Colander schemas with optional fields for which None is needed to store the absence of a value in ie a SQL database. For that, the colander.null value can not be used. So I made a supertype which can embrace every other schematype to add the posibility to add None as value.
Example:
typ = NoneType(Boolean())
serialize True --> 'true'
serialize None --> ''
deserialize '' --> None
deserlialize 'true' --> True