Skip to content
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

Closed
ahankinson opened this issue Nov 30, 2017 · 6 comments
Closed

No way to omit field from output #61

ahankinson opened this issue Nov 30, 2017 · 6 comments

Comments

@ahankinson
Copy link
Contributor

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:

  • Add an option to the field, omit_if_null
  • Add an option to the serializer initializer, omit_if_null
  • ???
@clarkduvall
Copy link
Owner

For your use case do you want to omit None values for all fields, or just omit None for certain fields?

@ahankinson
Copy link
Contributor Author

ahankinson commented Dec 3, 2017

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?

@clarkduvall
Copy link
Owner

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)

@artefactop
Copy link

I would like to have an option by field like:

class MySerializer(serpy.Serializer):
    foo = serpy.Field(required=False, omit_if_null=True)

@ahankinson
Copy link
Contributor Author

ahankinson commented Dec 18, 2017

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.

@hanshoi
Copy link

hanshoi commented May 14, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants