I want to get the humanized date in Django REST framework for an Android app to consume it.
My class is:
class Nota(models.Model):
titulo = models.CharField(max_length=45)
descripcion = models.TextField()as')
fecha = models.DateTimeField(auto_now_add=True)
def FORMAT(self):
return naturaltime(self.fecha)
def __unicode__(self):
return self.titulo
And my serialize is:
class NotaSerializer(serializers.ModelSerializer):
usuario = serializers.Field(source='usuario.username')
fecha = serializers.Field(source='FORMAT')
class Meta:
model = Nota
fields = ('usuario','subseccion','titulo','descripcion','imagen',
'fecha','longitud','latitud','imagen','likes', 'privacidad', )
But I get
"fecha": "1\u00a0day, 18\u00a0hours ago"
I want to get 1 day, 18 hours ago. How can I get the correct format?
Try with:
def FORMAT(self):
from django.utils.timesince import timesince
return timesince(self.fecha)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With