Hey guys, I have a JSON with unixtime in it. My Django database has datetime fields. I want JSON unixtime fields to be converted into datetime and inserted into database on POST request.
When doing GET, I want datetime fields to be converted into JSON with unixtime in it.
How I should perform such things.
class MySerializer(serializers.Serializer):
datecreated = serializers.SerializerMethodField()
def get_datecreated(self, obj):
return datetime.fromtimestamp(obj.dateCreated)
This is what I've done so far. It allows you to convert datetime Django field into unixtime and then into JSON.
How to do the opposite?
What approach should I use?
I've heard about this methods but it's to complicated to ovveride the whole representation while I need only I field to be converted.
def to_representation(self, obj):
def to_internal_value(self, data):