How to get django queryset results with formatted datetime field

后端 未结 3 1984
無奈伤痛
無奈伤痛 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:49

    Got the solution.

    data = list(Model.objects.extra(select={'date':"to_char(_.created_at, 'YYYY-MM-DD hh:mi AM')"}).values_list('date', flat='true')
    

    It's not just tablename.attribute, it should be dbname_tablename.attribute when we have multiple databases(ambiguous)

    which will result list of created_at datetime values trimmed to 'YYYY-MM-DD HH:MM' format.

提交回复
热议问题