serializers.py 447 B

123456789101112131415
  1. from datetime import datetime
  2. from django.utils.timezone import make_aware
  3. from rest_framework import serializers
  4. class FlexibleDateTimeField(serializers.DateTimeField):
  5. """ Supports both DateTime and unix epoch timestamp """
  6. def to_internal_value(self, timestamp):
  7. try:
  8. return make_aware(datetime.fromtimestamp(float(timestamp)))
  9. except ValueError:
  10. return super().to_internal_value(timestamp)