title: How to use default datetime serialization in Django REST Framework?
body: <p>I've got a Django REST Framework serializer containing the following:</p>
  
  <pre><code>from rest_framework import serializers
  
  
  class ThingSerializer(serializers.ModelSerializer):
      last_changed = serializers.SerializerMethodField(read_only=True)
  
      def get_last_changed(self, instance: Thing) -&gt; str:
          log_entry = LogEntry.objects.get_for_object(instance).latest()
  
          representation: str = serializers.DateTimeField('%Y-%m-%dT%H:%M:%SZ').to_representation(log_entry.timestamp)
          return representation
  </code></pre>
  
  <p>This is problematic because if the datetime formatting ever changes it will be different to all the other <code>datetime</code>s. I want to <strong>reuse the code path which DRF uses to serialize other <code>datetime</code> fields.</strong></p>
  
  <p>What I've tried so far:</p>
  
  <ul>
  <li>The only <a href="https://stackoverflow.com/a/5781657/96588">answer</a> which looked relevant doesn't actually produce the same result as DRF (it includes milliseconds, which DRF does not), presumably because it's using the Django rather than DRF serializer.</li>
  <li><code>rest_framework.serializers.DateTimeField().to_representation(log_entry.timestamp)</code>, <code>rest_framework.fields.DateTimeField().to_representation(log_entry.timestamp)</code> and <code>rest_framework.fields.DateTimeField(format=api_settings.DATETIME_FORMAT).to_representation(log_entry.timestamp)</code> don't work either; they produce strings with microsecond accuracy. I've verified with a debugger that DRF calls the latter when serializing other fields, so I can't understand why it produces a different result in my case.</li>
  <li><p><code>LogEntry.timestamp</code> is declared as a <code>django.db.DateTimeField</code>, but if I try something like <code>LogEntry.timestamp.to_representation(log_entry.timestamp)</code> it fails badly:</p>
  
  <blockquote>
    <p>AttributeError: 'DeferredAttribute' object has no attribute 'to_representation'</p>
  </blockquote></li>
  </ul>