python - How to get django queryset results with formatted datetime field -
i've django model has foreign keys associated other models. each model having same field names(attributes) created_at , updated_at
in every django queryset results i'll getting datetime values.
model.objects.all().values('created_at')
but want format datetime field "dd-mm-yyyy hh:mm:ss" , trim down milliseconds in django query results.
if use "extra" , and date_trunc_sql following command
dt = connection.ops.date_trunc_sql('day','created_date') objects.extra({'date':dt}).values('date')
which works fine. if query following, raising ambiguous statement error.
objects.extra({'date':dt}).values('date', 'x', 'y', 'z')
how overcome problem?
got solution.
data = list(model.objects.extra(select={'date':"to_char(<databasename>_<tablename>.created_at, 'yyyy-mm-dd hh:mi am')"}).values_list('date', flat='true')
it's not tablename.attribute, should dbname_tablename.attribute when have multiple databases(ambiguous)
which result list of created_at datetime values trimmed 'yyyy-mm-dd hh:mm' format.
Comments
Post a Comment