How i can resolve error "AttributeError: 'str' object has no attribute 'strftime'" and "TranslateModel object has no member named..."

Hi i would like to generate a printout after work with the wizard “credit”, I have remarked that the date of the invoice change automatically to today date(default_date) , But what i want in my printout that i would generated is display the origin date of the invoice that I have made already when i have posted the invoice by me.

I have added 2 methods to get this previous date , Here is the code:

1/I have defined a new field named “origins_date_invoice” on the class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin):

origins_date_invoice = fields.Function(fields.Date('Origins Date invoice'), 'get_origins_date_invoice')

2/then the method :get_origins_date_invoice

def get_origins_date_invoice(self, name):
    return ', '.join(set(filter(None,
                (str(l.origin_date) for l in self.lines)))).strftime("%d/%m/%Y")

and a method origin_date

    @property
    def origin_date(self):
        if isinstance(self.origin, self.__class__):
            return self.origin.invoice.invoice_date
        else:
            return " "

3/I do no really may be i have did a mistake there in this code , because when i have called this field to print it in my invoice_printout by this line of code:

<text:p text:style-name="P9"><text:span text:style-name="T1">Date:</text:span><text:span text:style-name="T2"> </text:span><text:span text:style-name="T2"><text:placeholder text:placeholder-type="text">&lt;format_date(record.origins_date_invoice, record.company.party.lang) if record.origins_date_invoice else format_date(datetime.date.today(), record.company.party.lang)&gt;</text:placeholder></text:span></text:p>

An error was displayed and this is the traceback:


  File "/lib/python3.7/site-packages/trytond/modules/account_invoice/invoice.py", line 1193, in get_origins_date_invoice
    (str(l.origin_date) for l in self.lines)))).strftime("%d/%m/%Y")
AttributeError: 'str' object has no attribute 'strftime'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/lib/python3.7/site-packages/genshi/template/eval.py", line 307, in lookup_attr
    val = obj[key]
TypeError: 'TranslateModel' object is not subscriptable

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/lib/python3.7/site-packages/genshi/template/eval.py", line 160, in evaluate
    return eval(self.code, _globals, {'__data__': data})
  File "<string>", line 1288, in <Expression '__relatorio_escape_invalid_chars(format_date(record.origins_date_invoice, record.company.party.lang) if record.origins_date_invoice else format_date(datetime.date.today(), record.company.party.lang))'>
  File "/lib/python3.7/site-packages/genshi/template/eval.py", line 309, in lookup_attr
    val = cls.undefined(key, owner=obj)
  File "/lib/python3.7/site-packages/genshi/template/eval.py", line 397, in undefined
    raise UndefinedError(key, owner=owner)
genshi.template.eval.UndefinedError: <trytond.report.report.Report._get_records.<locals>.TranslateModel object at 0x7f9fcf856668> has no member named "origins_date_invoice"

How i can solve that, Thanks in advance.

The problem is that the fallback value of your origin_date property returns a string with a space which can not be used as value for format_date (but is evaluated as True).
You should return a date instance or for example None .

Hi Mr.@ced , If you mean that i should try replace (return " " by return None) better —>I have tried to do that like this in the origin_date property :

@property
def origin_date(self):
    if isinstance(self.origin, self.__class__):
        return self.origin.invoice.invoice_date
    else:
        return None

But the same error displayed.

Here you are trying to apply the strftime function to the joined string which will contain something like ‘2022-03-01, 2022-04-15, 2022-01-21’.
So, I think this should probably be something more like:

def get_origins_date_invoice(self, name):
    origin_dates = set(filter(None, (l.origin_date for l in self.lines)))
    return ', '.join(d.strftime('%d/%m/%Y') for d in origin_dates)

Thanks Mr@dave , I have tried your solution But it gives me now another error:

  File "/lib/python3.7/site-packages/trytond/ir/lang.py", line 550, in strftime
    return value.strftime(format)
AttributeError: 'str' object has no attribute 'strftime'

This may be because you have defined the origins_date_invoice field as fields.Date, but you are returning a string. So changing that to a fields.Char might fix that.

thanks , yeah i tried that also and keeping gives me AttributeError: 'str' object has no attribute 'strftime', is there any other way to took this date ?

If you have changed origins_date_invoice to a fields.Char, then it makes no sense to try and format it as a date.