-
Notifications
You must be signed in to change notification settings - Fork 60
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
No way to omit field from output #61
Comments
For your use case do you want to omit None values for all fields, or just omit None for certain fields? |
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 The behaviour in my use case is to omit the field entirely from the output if the value is |
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 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) |
I would like to have an option by field like: class MySerializer(serpy.Serializer):
foo = serpy.Field(required=False, omit_if_null=True) |
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. |
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 Would a PR for that kind of thing be appreciated or does adding such configuration just bloat the code? |
With the reversion of #38, it seems there is currently no way to omit a value from the serialized output. I'm happy to submit a PR, but would appreciate some direction to know what would be an appropriate solution:
omit_if_null
omit_if_null
The text was updated successfully, but these errors were encountered: