简体   繁体   中英

TypeError: object of type 'bool' has no len() - Odoo v9 community

I still have this error on another part of the code:

class invoice(models.Model):
_inherit = "account.invoice"

@api.multi
def send_xml_file(self):
    # haciendolo para efacturadelsur solamente por ahora
    host = 'https://www.efacturadelsur.cl'
    post = '/ws/DTE.asmx' # HTTP/1.1
    url = host + post
    _logger.info('URL to be used %s' % url)
    # client = Client(url)
    # _logger.info(client)
    _logger.info('len (como viene): %s' % len(self.sii_xml_request))

    response = pool.urlopen('POST', url, headers={
        'Content-Type': 'application/soap+xml',
        'charset': 'utf-8',
        'Content-Length': len(
            self.sii_xml_request)}, body=self.sii_xml_request)

    _logger.info(response.status)
    _logger.info(response.data)
    self.sii_xml_response = response.data
    self.sii_result = 'Enviado'

Before in my previous question the error was solved on this line:

_logger.info('len (como viene): %s' % (len(self.sii_xml_request) if self.sii_xml_request else '')

Now I have it again on the next one, I've tried with a conditional like before, but I still can't solve it, must be related to syntax or something, the error is on this sentence:

        response = pool.urlopen('POST', url, headers={
        'Content-Type': 'application/soap+xml',
        'charset': 'utf-8',
        'Content-Length': len(
            self.sii_xml_request)}, body=self.sii_xml_request)

Specifically on self.sii_xml_request)}, body=self.sii_xml_request) there's the sii_xml_request object again, I think is just a matter to add the conditional, since the field is empty...

But I still can't make it work properly, is this solvable in a similar fashion as my previous question?

Thanks in advance!

EDIT

It is not a duplicate since this is another line of code, and a very very similar way to solve it won't apply here, it is a slightly different syntax.

SECOND EDIT

This is how it looks right now, the conditional is on every len of this function

@api.multi
def send_xml_file(self):
    # haciendolo para efacturadelsur solamente por ahora
    host = 'https://www.efacturadelsur.cl'
    post = '/ws/DTE.asmx' # HTTP/1.1
    url = host + post
    _logger.info('URL to be used %s' % url)
    # client = Client(url)
    # _logger.info(client)
    _logger.info('len (como viene): %s' % len(self.sii_xml_request)if self.sii_xml_request else '')
    #if self.sii_xml_request:
    response = pool.urlopen('POST', url, headers={
        'Content-Type': 'application/soap+xml',
        'charset': 'utf-8',
        'Content-Length': (len(
            self.sii_xml_request) if self.sii_xml_request else '')}, body=self.sii_xml_request)
    #else ''(len(self.sii_xml_request) if self.sii_xml_request else '') 

    _logger.info(response.status)
    _logger.info(response.data)
    self.sii_xml_response = response.data
    self.sii_result = 'Enviado'

To avoid dragging on the conversation in the comments, I am going to take a crack at an actual answer.

It seems like your object self.sii_xml_request can be either a). a string, or b). a boolean ( True or False ) (though please correct me if I am wrong).

You are getting an error, because you are trying to take the len() of that object to get an idea of the length of the request, but when that object is True or False this will fail, because bool objects don't have a __len__ attribute. You tried solving this based on a previous question by doing this instead:

(len(self.sii_xml_request) if self.sii_xml_request else '')

This will only work if self.sii_xml_request only ever returns a string or False (or something that is equivalent to False like None or 0 or [] , etc.), because if it returns True , then it will once again try to get the len() of the object which doesn't work.

Doing:

(len(self.sii_xml_request) if self.sii_xml_request is not True or False else '')

Might work, but I don't know what decides whether self.sii_xml_request returns True , False or some string, and you may want to handle True and False differently. Also, you probably never want to have content length be '' because it will normally be an integer, so if anything you should have it be 0 if self.sii_xml_request is False . If you want to handle them the same try what I have above. Otherwise, you could define a variable content_length earlier, and set it accordingly based on the value of self.sii_xml_request . For example:

if isinstance(self.sii_xml_request, bool):
    content_lengthj == int(self.sii_xml_request)  # 1 if True else 0
else:
    content_length = len(self.sii_xml_request)

...
response = pool.urlopen('POST', url, headers={
        'Content-Type': 'application/soap+xml',
        'charset': 'utf-8',
        'Content-Length': content_length}, body=self.sii_xml_request)

this kind of error when you get 'bool' in the message that means that you calling a function on an empty field so before you call any function check if the field has value first. because in odoo empty field contain False value not None i had this error many times every time i found out that i'm calling the function on an empty field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM