Home > Enterprise >  Manipulate with value list in Django
Manipulate with value list in Django

Time:01-27

I have a value list in Django

maca = Orders.objects.all().values_list("order_date")

But I want to manipulate with order date. It shows up as YYYY-MM-DD and I want to change the format.

For example

maca = Orders.objects.all().values_list(datetime.strftime("order_date", "%d/%m/%Y")

It is not only this case.

Thanks a lot

CodePudding user response:

If order_date is a DateField, you're getting a Python datetime.date, whose default string format is ISO8601 YYYY-MM-DD.

If you want a different format in templates, use the date filter:

{% for date in maca %}
  {{ date|date }}
{% endfor %}

If you want to just use e.g. strftime, that's just basic Python. Be sure to add flat=True to values_list to get a list of dates, not a list of 1-tuples containing dates.

maca = [
    date.strftime("%d/%m/%Y")
    for date
    in Orders.objects.all().values_list("order_date", flat=True)
]
  •  Tags:  
  • Related