How to get django queryset results with formatted datetime field

后端 未结 3 1995
無奈伤痛
無奈伤痛 2021-01-18 12:14

I\'ve Django model which has foreign keys associated with other models. Each model is having same field names(attributes) created_at and updated_at

In every django q

3条回答
  •  没有蜡笔的小新
    2021-01-18 12:37

    Solved it via @Yannics answer at: https://stackoverflow.com/a/60924664/5804947

    This also avoids using extra which should be "a last resort" due to Django docs.

    from django.db.models import F, Func, Value, CharField
    
    qs.annotate(
      formatted_date=Func(
        F('created_at'),
        Value('DD-MM-YYYY HH:MM:SS'),
        function='to_char',
        output_field=CharField()
      )
    )
    

提交回复
热议问题