1
activitylist = sr.activity_set.all()
cell.paragraphs[0].text = activitylist.values_list('activityPlannedStartDate', flat = True)

I am getting an error of: 'in ' requires string as left operand, not datetime.date

cell.paragraphs[0].text is a cell in a predefined table in a docx that I build in Word.

Please help with this TypeError.

Thank you in advance!

karthikr
  • 97,368
  • 26
  • 197
  • 188

2 Answers2

1

You should use strftime method to format date to string. And use string.join to represent values list as a comma-separated string:

formatted_dates = [date.strftime("%Y-%m-%d") for date in
                         activitylist.values_list('activityPlannedStartDate',
                                                  flat=True)]
cell.paragraphs[0].text = ', '.join(formatted_dates)
catavaran
  • 44,703
  • 8
  • 98
  • 85
0
from django.db.models import F, Func, Value, CharField

qs.annotate(
  formatted_date=Func(
    F('date'),
    Value('dd.MM.yyyy hh:mm'),
    function='to_char',
    output_field=CharField()
  )
)

This works only with a database that supports the to_char date type formatting function. Postgres provides this function by default. If you use a MSSQL backend you could swap to_char with FORMAT. For Oracle consult their documentation, etc.

After the queryset is evaluated this will add the annotation formatted_date to each object in the queryset that is returned.

[obj.formatted_date for obj in qs]
Yannic Hamann
  • 4,655
  • 32
  • 50