Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get "humanized" date in Django REST Framework

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?

like image 467
Cris_Towi Avatar asked Sep 30 '14 23:09

Cris_Towi


1 Answers

Try with:

   def FORMAT(self):
       from django.utils.timesince import timesince
       return timesince(self.fecha)
like image 50
Venkatesh Bachu Avatar answered Oct 12 '22 02:10

Venkatesh Bachu