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 disabling of declared fields on serializer subclasses #4764
Conversation
Side note - some of the CI jobs for the first commit passed because I pushed the second commit while the whole build was running. This is related to travis-ci/travis-ci#6337 |
Nice change! We'll want to add some docs corresponding to this, perhaps in a subsection underneath this?... http://www.django-rest-framework.org/api-guide/serializers/#advanced-serializer-usage |
I noticed that there wasn't a section that generally covered serializer inheritance, so I kind of rolled the docs request into the existing
|
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.
Looks great. An example in the docs and I think this is fully cooked.
@@ -1025,6 +1015,32 @@ If any of the validation fails, then the method should raise a `serializers.Vali | |||
|
|||
The `data` argument passed to this method will normally be the value of `request.data`, so the datatype it provides will depend on the parser classes you have configured for your API. | |||
|
|||
## Serializer Inheritance |
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.
I like the extra section here - the notes on Meta
are worth it.
Additionally, the following caveats apply to serializer inheritance: | ||
|
||
* Normal Python name resolution rules apply. If you have multiple base classes that declare a `Meta` inner class, only the first one will be used. This means the child’s `Meta`, if it exists, otherwise the `Meta` of the first parent, etc. | ||
* It’s possible to declaratively remove a `Field` inherited from a parent class by setting the name to be `None` on the subclass. You can only use this technique to opt out from a field defined declaratively by a parent class; it won’t prevent the `ModelSerializer` from generating a default field. To opt-out from default fields, see [Specifying which fields to include](#specifying-which-fields-to-include). |
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.
Let's include an example for this.
Fabulous! |
This corresponds to a similar PR for django-filter. In short, the django form API supports a syntax for disabling fields on child forms (django ticket), but this does not work on serializers.
Example: