#36986: Add a hook to Serializer._value_from_field() for complex fields like
CompositePrimaryKey
-------------------------------------+-------------------------------------
     Reporter:  Tim Graham           |                    Owner:  (none)
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Core                 |                  Version:  dev
  (Serialization)                    |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Vishy Algo):

 Could we do a protocol-based hook similar to this?

 {{{#!diff
 diff --git a/django/core/serializers/python.py
 b/django/core/serializers/python.py
 index 73ba24368b..b2e61d1b72 100644
 --- a/django/core/serializers/python.py
 +++ b/django/core/serializers/python.py
 @@ -40,6 +40,8 @@ class Serializer(base.Serializer):
      return data

      def _value_from_field(self, obj, field):
 +        if hasattr(field, 'get_serializer_value'):
 +            return field.get_serializer_value(obj)
          if isinstance(field, CompositePrimaryKey):
              return [self._value_from_field(obj, f) for f in field]
          value = field.value_from_object(obj)
 }}}

 I guess, this gives custom fields full control while keeping it generic.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36986#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019cf29be856-63c7de80-ba8e-43cb-92f8-aaef4813843a-000000%40eu-central-1.amazonses.com.

Reply via email to