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

**URL:** https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051
**Category:** Developer
**Created:** [March 1, 2022, 1:27pm UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051 "2022-03-01T13:27:36Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)
#### Post date: [March 1, 2022, 1:27pm UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051/1 "2022-03-01T13:27:37Z")

</div>

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**

```auto
    @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:

```auto

  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.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [March 1, 2022, 4:54pm UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051/2 "2022-03-01T16:54:10Z")

</div>

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` .

---

<div class="post-metadata">

### Author: ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)
#### Post date: [March 1, 2022, 9:27pm UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051/3 "2022-03-01T21:27:33Z")

</div>

> [@ced](#):
>
> `origin_date` property returns a string with a space

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.

---

<div class="post-metadata">

### Author: ![dave](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://discuss.tryton.org/u/dave)
#### Post date: [March 1, 2022, 10:34pm UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051/4 "2022-03-01T22:34:46Z")

</div>

> [@mimo\_Alva](#):
>
> 2/then the method : **get\_origins\_date\_invoice**
> 
> ```auto
> 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")
> 
> ```

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)

```

---

<div class="post-metadata">

### Author: ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)
#### Post date: [March 2, 2022, 12:46am UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051/5 "2022-03-02T00:46:03Z")

</div>

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

```auto
  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'

```

---

<div class="post-metadata">

### Author: ![dave](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://discuss.tryton.org/u/dave)
#### Post date: [March 3, 2022, 9:59am UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051/6 "2022-03-03T09:59:58Z")

</div>

> [@mimo\_Alva](#):
>
> origins\_date\_invoice = fields.Function(fields.Date(‘Origins Date invoice’), ‘get\_origins\_date\_invoice’)

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.

---

<div class="post-metadata">

### Author: ![mimo\_Alva](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@mimo\_Alva](https://discuss.tryton.org/u/mimo_Alva)
#### Post date: [March 4, 2022, 2:51pm UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051/7 "2022-03-04T14:51:41Z")

</div>

> [@mimo\_Alva](#):
>
> \<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”\>\<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)\>\</text:placeholder\>\</text:span\>\</text:p\>

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 ?

---

<div class="post-metadata">

### Author: ![dave](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://discuss.tryton.org/u/dave)
#### Post date: [March 4, 2022, 2:56pm UTC](https://discuss.tryton.org/t/how-i-can-resolve-error-attributeerror-str-object-has-no-attribute-strftime-and-translatemodel-object-has-no-member-named/5051/8 "2022-03-04T14:56:26Z")

</div>

> [@mimo\_Alva](#):
>
> format\_date(record.origins\_date\_invoice, record.company.party.lang)

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