TypeError: 'datetime.timedelta' object is not iterable

Hi,
I want to generate a printout for the module timesheet**(the window Lines)** [timesheet/menu---->Lines],

and one of the fields that i want to display is the sum of the duration on the printout, I have called the duration of each line then i have applied the sum directly to those lines and Here is the code:


     <table:table-cell table:style-name="Tabelle1.A3" table:number-columns-spanned="2" office:value-type="string">
      <text:p text:style-name="P30">Sum:</text:p>
     </table:table-cell>
     <table:covered-table-cell/>
     <table:covered-table-cell/>     
     <table:table-cell table:style-name="Tabelle1.A3" table:number-columns-spanned="2" office:value-type="string">
      <text:p text:style-name="P31"><text:placeholder text:placeholder-type="text">&lt;for each=&quot;line in timesheet.lines&quot;&gt;</text:placeholder></text:p>
      <text:p text:style-name="P31"><text:placeholder text:placeholder-type="text">&lt;format_duration(sum(line.duration), line.company.party.lang) if line.duration else " "&gt;</text:placeholder></text:p>
      <text:p text:style-name="P31"><text:placeholder text:placeholder-type="text">&lt;/for&gt;</text:placeholder></text:p>
     </table:table-cell>

But there was an error tell me:

  File "/site-packages/genshi/template/eval.py", line 160, in evaluate
    return eval(self.code, _globals, {'__data__': data})
  File "<string>", line 485, in <Expression 'format_duration(sum(line.duration), line.company.party.lang) if line.duration else " "'>
TypeError: 'datetime.timedelta' object is not iterable

```How i can solve that to reach the goal of displaying the sum of the duration, Thanks in advance.

Sum accepts a list of arguments (not a single value).
If you want to sum the duration of all records you should do something like:

sum(line.duration for line in records)

Hope this helps

Hi, THanks Mr.@pokoli;

I have change the code ,

But this time give me another error has no member named “duration”`:

genshi.template.eval.UndefinedError: <trytond.report.report.Report._get_records.<locals>.TranslateModel object at 0x7f40ab45b080> has no member named "duration"

Here is the code after the modification :

     <table:table-cell table:style-name="Tabelle1.A3" table:number-columns-spanned="2" office:value-type="string">
      <text:p text:style-name="P30">Summe:</text:p>
      <text:p text:style-name="P31"><text:placeholder text:placeholder-type="text">&lt;for each=&quot;line in timesheet.lines&quot;&gt;</text:placeholder></text:p>
      <text:p text:style-name="P31"><text:placeholder text:placeholder-type="text">&lt;format_duration(sum(line.duration for line in records))&gt;</text:placeholder></text:p>
      <text:p text:style-name="P31"><text:placeholder text:placeholder-type="text">&lt;/for&gt;</text:placeholder></text:p>
    </table:table-cell>


and this is the method **get_context**

    @classmethod
    def get_context(cls, records, data):
        pool = Pool()
        TimesheetLine = pool.get('line.printable') 
        context = Transaction().context
        report_context = super(TimesheetLineReport, cls).get_context(records, data)
        report_context['timesheet'] = report_context['record']
        report_context['format_duration'] = cls.format_duration
        return report_context

There is no need to include a for loop in relatorio (because this will produce a single text for each line). THe for loop is done in the calculation of the expression, so relation will sum all the values and just print a single value.

The following code should print what you expect:

     <table:table-cell table:style-name="Tabelle1.A3" table:number-columns-spanned="2" office:value-type="string">
      <text:p text:style-name="P30">Summe:</text:p>
      <text:p text:style-name="P31"><text:placeholder text:placeholder-type="text">&lt;format_duration(sum(line.duration for line in timesheet.lines))&gt;</text:placeholder></text:p>
    </table:table-cell>
1 Like

Thanks again, but it seems now that it see the duration as an integer and not as a time

Here is the error displayed:

`  File "<string>", line 480, in <Expression '__relatorio_guess_type(__relatorio_store_cache(140239495126792, format_duration(sum(line.duration for line in timesheet.lines))))'>
TypeError: unsupported operand type(s) for +: 'int' and 'datetime.timedelta'`