Opened 7 years ago
Last modified 14 months ago
#29522 closed Cleanup/optimization
Serializers are hard to modify — at Version 1
| Reported by: | Levi Cameron | Owned by: | nobody |
|---|---|---|---|
| Component: | Core (Serialization) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Herbert Fortes, Ian Foote, Ülgen Sarıkavak | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Use case
Three times now I've had need to modify the ORM serializers for project use. In every case I've had to replicate large parts (if not all) of django.core.serializers.base in order to change 1 or 2 lines because certain calls to functions in django.core.serializers.base from the serializers are hardcoded.
- Changing JSON serialization to output in alphabetical order (so that
dumpdatadoesn't reorder every field each time you run it) - Changing deserialization to use
_base_managerinstead of_default_manager(due to extra constraints on a custom manager) - Implementing fixes for #7202, #7350 before they were merged
Proposal
I propose that instead of calling the following functions directly, the serializer classes are modified to call an instance method of the same name that then calls the existing module-level functions. This would allow the ability to more easily override just one aspect of serializer functionality without having to replicate large swathes of code.
I am not proposing to move these functions into the python serializer class directly because this may break compatibility with existing code that depends on the existing functions.
django.core.serializers.base.build_instancedjango.core.serializers.base.deserializer_m2m_valuesdjango.core.serializers.base.deserializer_fk_valuesdjango.core.serializers.python._get_model(given that this is already an internal function, implementation can be pushed into the class directly)
(It might also be nice if the json and pyyaml classes could also have PythonDeserializer be made a class attribute but that would involve turning the Deserializer function into a class and is not nearly as much code to override these as the functions above)
If this sounds acceptable, I will submit a patch.