I just started using Django REST framework and need to customize a DATETIME field to unix epoch (integer) when serializing it
i currently have
/models.py
class Snippet(models.Model):
created = models.DateTimeField(auto_now_add=True)
/serializers.py
class SnippetSerializer(serializers.ModelSerializer):
class Meta:
model = Snippet
fields = ('created')
When I call snippets through API, i get typical DATETIME in STR format .
How can I change SnippetSerializer to convert to EPOCH. Also, when someone is posting information, it will come back to me in EPOCH, which I would need to convert to DATETIME . Any ideas?
Please see below code it will help you to solve your problem.