master branch of Django REST framework.DateTimeFieldclass DateSerializer(serializers.Serializer):
date = fields.DateTimeField()
python shell
>> a = DateSerializer(data={'date':'1508239944313'})
is_valid()There should be a way to validate unix epoch timestamp by using DateTimeField by including an extra input_formats argument like input_formats=['epoch_timestamp']
I think this is just a limitation of the underlying strftime() and strptime() Behavior. There's no format for Unix Time timestamps there.
Instead you'll need to create a custom field using datetime.datetime.fromtimestamp() (or similar) in to_internal_value.
@carltongibson I can make a patch for DRF to support this input format in DateTimeField. We can use datetime.datetime.fromtimestamp() to parse the timestamp input in DateTimeField.
What do you think?
I think this is better in user-land code. A custom field would be simple enough.
I also agree. But seems like this format can also be included in the DateTimeField as this format is commonly used.
I'm -0 on this change to DateTimeField. If this were to be accepted, I'd rather see it added as a separate TimestampField or something similar.
please support epoch timestamp too!
@rpkilby @carltongibson Having something like TimestampField that serializes and deserializes the unix epoch timestamps is a very common use case, and providing support for it would be better than delegating it to user-land code.
I can raise a pull request if you are interested.
Most helpful comment
@rpkilby @carltongibson Having something like
TimestampFieldthat serializes and deserializes the unix epoch timestamps is a very common use case, and providing support for it would be better than delegating it to user-land code.I can raise a pull request if you are interested.