Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
2891 มุมมอง

If I do:

if shipment_obj.move_lines:
    for line in shipment_obj.move_lines: 
        #line.name, line.id etc..

this works no problem, but I shouldn't need the if statement.

If I just do:

    for line in shipment_obj.move_lines: 
       #line.name, line.id etc..

this throws: "TypeError: 'NoneType' object is not iterable"

Just want to know, why this could be?

here for reference:

shipment_obj = self.pool.get('stock.picking.in').browse(cr, uid, context.get('active_id')) #gives correct shipment obj

trying to access:

 'move_lines': fields.one2many('stock.move', 'picking_id', 'Internal Moves', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]})
อวตาร
ละทิ้ง

Does it really work when using "if", or does it just not throw errors?

ผู้เขียน

it just throws errors when I don't use the if, its absolutely crazy I know.

ผู้เขียน

yes when I use the if it works fine

ผู้เขียน

found the problem. sorry, it is the way I thought the method was called that didn't need to check context and active_id since there would always be a context and active_id, I was wrong.

ผู้เขียน คำตอบที่ดีที่สุด

found the problem, context needs to be checked as well as active_id:

 if context is not None and context.get('active_id'):

...it is the way I thought the method was called that didn't need to check context and active_id since there would always be a context and active_id, I was wrong.

อวตาร
ละทิ้ง

The devil was in the details, as usual...

ผู้เขียน

thanks for your help.

คำตอบที่ดีที่สุด

If there is no move_lines, it is False, not None.

Try instead:

if shipment_obj.move_lines:
    for line in shipment_obj.move_lines: 
        #line.name, line.id etc..
อวตาร
ละทิ้ง