serializers.py 445 B

1234567891011121314
  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, value):
  7. try:
  8. return make_aware(datetime.fromtimestamp(float(value)))
  9. except (ValueError, TypeError):
  10. return super().to_internal_value(value)