Opened 2 years ago

Last modified 16 months ago

#29522 assigned Cleanup/optimization

Make Serializers easier to modify.

Reported by: Levi Cameron Owned by: Emad Mokhtar
Component: Core (Serialization) Version: master
Severity: Normal Keywords:
Cc: Herbert Fortes, Ian Foote Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Levi Cameron)

Use case

Three times now I've had need to modify the ORM serializers for project use. In some cases I've had to replicate large parts (if not most) of django.core.serializers.python 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 dumpdata doesn't reorder every field each time you run it)
  • Changing deserialization to use _base_manager instead 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_instance
  • django.core.serializers.base.deserializer_m2m_values
  • django.core.serializers.base.deserializer_fk_values
  • django.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.

Change History (11)

comment:1 Changed 2 years ago by Levi Cameron

Description: modified (diff)

comment:2 Changed 2 years ago by Levi Cameron

Description: modified (diff)

comment:3 Changed 2 years ago by Levi Cameron

Description: modified (diff)

comment:4 Changed 2 years ago by Carlton Gibson

Triage Stage: UnreviewedAccepted

This sounds reasonable to me. My main thought would be to make sure the new hooks a properly documented etc. But yes, good idea.

comment:5 Changed 2 years ago by Carlton Gibson

Summary: Serializers are hard to modifyMake Serializers easier to modify

comment:6 Changed 2 years ago by Herbert Fortes

Cc: Herbert Fortes added

comment:7 Changed 16 months ago by Emad Mokhtar

Owner: changed from nobody to Emad Mokhtar
Status: newassigned

comment:8 Changed 16 months ago by Ian Foote

Cc: Ian Foote added

comment:9 Changed 16 months ago by Emad Mokhtar

Last edited 16 months ago by felixxm (previous) (diff)

comment:10 Changed 16 months ago by Ian Foote

Has patch: set

comment:11 Changed 16 months ago by felixxm

Needs tests: set
Patch needs improvement: set
Summary: Make Serializers easier to modifyMake Serializers easier to modify.
Note: See TracTickets for help on using tickets.
Back to Top