'TypeError: sequence item 0: expected str instance, datetime.date found

I want to get a date of an origin invoice after doing a special process to this invoice because the first date changed when i try to do this process but i faced this error when am trying to doing this process :

 in get_origins_date_invoice TypeError: sequence item 0: expected str instance, datetime.date found.

this is the code to get my origin date:

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

def get_origins_date_invoice(self, name):
    return ', '.join(set(filter(None,
                (l.origin_date for l in self.lines))))

and this is the error displayed:

in get_origins_date_invoice
    (l.origin_date for l in self.lines))))
TypeError: sequence item 0: expected str instance, datetime.date found.

How can i solve that please.



Solution 1:[1]

The join() method of the built-in str type in Python accepts iterables returning type str, so the path in origin_date() with a non-string return type is incompatible with this.

To fix this, you can either change origin_date() like this:

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

... or change get_origins_date_invoice() like this:

def get_origins_date_invoice(self, name):
    return ', '.join(set(filter(None,
                (str(l.origin_date) for l in self.lines))))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 constantstranger