Can I change the default __str__()
function of the datetime.datetime object?
By default, it returns something like '2010-06-28 12:43:56.985790'
, and i need it to print something like '2010-06-28T12:44:21.241228'
(which is the isoformat() function).
I need this for JSON serializing of a django model.
My model is:
class Transport(models.Model):
user = models.ForeignKey(User)
source = models.ForeignKey(Marker, related_name="source_marker")
destination = models.ForeignKey(Marker, related_name="destination_marker")
object = models.CharField(choices=possesion_choices, max_length=2**6)
quantity = models.IntegerField()
time_sent = models.DateTimeField()
time_arrived = models.DateTimeField()
And when i serialize it (using the wadofstuff module), it prints something like
print serializers.serialize('json', Transport.objects.all(), relations=('source', 'destination',) indent=4)
[
{
"pk": 1,
"model": "main.transport",
"fields": {
[.. bla bla ..]
"time_sent": "2010-06-28 12:18:05",
"time_arrived": "2010-06-28 12:38:36",
[.. bla bla ..]
}
}
]