Comments (6)
For your use case do you want to omit None values for all fields, or just omit None for certain fields?
from serpy.
In the specification I am writing against, a key with a value of 'null' is required to be omitted from the output.
So if we have {"foo": "bar", "data": "something"}
the output should be {"foo": "bar", "data": "something"}
. But if we have {"foo": "bar", "data": None}
the output should be {"foo": "bar"}
.
The behaviour in my use case is to omit the field entirely from the output if the value is null
. I would have thought required=False
would trigger that behaviour, but I think there's some uncertainty about what component of the value is "required." Is it "required" on the input data? Or "required" to be in the output serialization?
from serpy.
Required means it is required to have the value in the input data. So for example if you were trying to serialize the "data" with a standard serpy.Field(required=True)
, and you gave it {}
that would throw an error, but {'data': None}
would be fine.
I think for your case, you can make a custom serializer that all your serializers will inherit from, that has something like this to filter out None values:
class MySerializer(serpy.Serializer):
def remove_none(self, d):
return {k: v for k, v in d.items() if v is not None}
def to_value(self, instance):
v = super(MySerializer, self).to_value(instance)
if self.many:
return map(self.remove_none, v)
return self.remove_none(v)
from serpy.
I would like to have an option by field like:
class MySerializer(serpy.Serializer):
foo = serpy.Field(required=False, omit_if_null=True)
from serpy.
Sure, that would work. I guess I was looking for a solution where I didn't have to re-loop back over the output dictionary to filter out the values I didn't want, and instead prevent them from being in the output in the first place.
from serpy.
I'm having similar use case but instead to have it field by field it could be as a configuration for Serializer or serpy. What I was thinking that the value could be definable by yourself as there might be situations where you would wan't to have None
explicitly as value but then a different type for undefined.
Would a PR for that kind of thing be appreciated or does adding such configuration just bloat the code?
from serpy.
Related Issues (20)
- Can't install serpy from source
- Remove Python 2.6 from testing matrix HOT 1
- Methods are not overridden by the child class's implementation HOT 3
- Patch for compatibility with RelatedManager (reverse foreign-key) HOT 2
- Inconsistent: child serializer required=False returns None HOT 2
- Exceptions are not easy to trace
- DateField is not supported HOT 2
- Non-required key raises key error when not present HOT 2
- Expose fields such that swagger can autogenerate models/code? HOT 2
- Serializing properties which may or may not be present in object being passed in HOT 2
- serpy no longer supports None as field values HOT 1
- Nested 'self' field in Serializer HOT 2
- Accessing request object in method field - solution not working?
- Is it still under active development? HOT 2
- include test suite in PyPi tarball
- Django GeoJSON / Geometry fields. HOT 1
- Serpy does not catch Key and Attribute errors on MethodField
- is this library no longer maintained ? HOT 1
- Type Hints
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serpy.