简体   繁体   中英

How to get datetimefield as string in django queryset's values_list() function?

I have a model which contains a datetime field. I need to write the rows from that model table to excel sheet.

As the model field is datetime field, when writing to the excel, it's writing a number like 45976 etc for dates instead of 2020-04-01 as string.

I'm getting values of rows using queryset.values_list(*fields_to_fetch) This fields_to_fetch contains the datetimefield I'm looking for. When I print the type, it is saying DateTimeField in the console.

Is there any way to get the datetimefield as string type?

I was able to convert each item in the values_list() to list and then the datetimefield into string and append it to a new list and write to excel.

I'm looking for a way to avoid all this.

Use Cast(...) database function,

from django.db.models.functions import Cast
from django.db.models import CharField


data = ModelKlass.objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM