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

Should missing dictionary keys be required when serializing mappings. #2342

Closed
tomchristie opened this issue Dec 22, 2014 · 2 comments
Closed

Comments

@tomchristie
Copy link
Member

Eg see confusion here: http://stackoverflow.com/questions/27603669/django-rest-framework-3-required-field-behaviour

The 'required=False' flag has always been for if input is required when validating. For output we always require all fields to be present on the object.

For regular object this works fine - even if an attribute is unset, it will at least still exist. With mappings however the key may simply be missing. What's particularly odd here is that input data could successfully validate, but then 'serializer.data' could still raise an error as one of the fields could have been non-required.

Ideally we don't want to silently ignore missing attributes as that's likely to be eg a misnamed field and it shouldn't simply be blank. However for dictionaries, perhaps we should silently skip missing keys on output?

@kevin-brown
Copy link
Member

I would rather not special case dictionaries if we can, as I believe it's a valid issue for objects with missing attributes as well (that aren't pre-filled, like non-model objects).

My main question for this is what should we assume the default value is? I'm inclined to lead towards something with the default argument here, but then I think that ignores the required=False problem.

@tomchristie
Copy link
Member Author

My main question for this is what should we assume the default value is?

Omitting the field entirely would make sense. I'm still leaning towards allowing omission from dicts but not from objects. Again, missing attributes will indicate user errors, missing dictionary keys can occur in otherwise totally valid REST framework code.

@tomchristie tomchristie modified the milestone: 3.0.3 Release Dec 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants