仅检查最后一行而不是整行

时间:2017-11-27 04:07:12

标签: python python-2.7 openerp odoo-10

这是python Odoo代码,用于交叉检查两个单独类的每一行。但它只显示最后一行检查而不是整个行和列!任何人都可以帮助我!

@api.multi
    def compare_gstr(self):
        for gstr_file in self:
            err={}
            result=[]

            gstr_upload = self.env['bi.gstr.upload'].search([('gstr_upload_id','=', gstr_file.id)])
            gstr = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id)])
            for details in gstr_upload:
                # for det in gstr:
                if details.gstin:
                    gstin = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id),('gst_recipient','=', details.gstin)])
                    if not gstin:
                        err['GST Number']=str(details.gstin)+' Missmatch'

                if details.b2b_inv_inum:
                    b2b_inv_inum = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id),('invoice_ids','=', details.b2b_inv_inum)])
                    if not b2b_inv_inum:
                        err['Invoice Number']=str(details.b2b_inv_inum)+' Missmatch'

                if details.b2b_inv_idt:
                    b2b_inv_idt = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id),('invoice_date','=', details.b2b_inv_idt)])
                    if not b2b_inv_idt:
                        err['Invoice Date']=str(details.b2b_inv_idt)+' Missmatch'

                if details.b2b_inv_val:
                    b2b_inv_val = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id),('invoice_value','=', details.b2b_inv_val)])
                    if not b2b_inv_val:
                        err['Invoice Value']=str(details.b2b_inv_val)+' Missmatch'

                result.append(err)

            raise UserError(_(str(err)))

1 个答案:

答案 0 :(得分:0)

如果您要验证gstr_file行,请将代码更改为:

@api.multi
def compare_gstr(self):
    # put the list of error here
    result=[]
    for gstr_file in self:
        # list of errors of details
        details_errors = []
        gstr_upload = self.env['bi.gstr.upload'].search([('gstr_upload_id','=', gstr_file.id)])
        gstr = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id)])
        for details in gstr_upload:
            # you are getting one single erro be cause you always use the same
            # dictionary for every record
            # every row save it's error here
            err={}
            # for det in gstr:
            if details.gstin:
                gstin = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id),('gst_recipient','=', details.gstin)])
                if not gstin:
                    err['GST Number']=str(details.gstin)+' Missmatch'

            if details.b2b_inv_inum:
                b2b_inv_inum = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id),('invoice_ids','=', details.b2b_inv_inum)])
                if not b2b_inv_inum:
                    err['Invoice Number']=str(details.b2b_inv_inum)+' Missmatch'

            if details.b2b_inv_idt:
                b2b_inv_idt = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id),('invoice_date','=', details.b2b_inv_idt)])
                if not b2b_inv_idt:
                    err['Invoice Date']=str(details.b2b_inv_idt)+' Missmatch'

            if details.b2b_inv_val:
                b2b_inv_val = self.env['bi.gstr'].search([('gstr2_id','=', gstr_file.id),('invoice_value','=', details.b2b_inv_val)])
                if not b2b_inv_val:
                    err['Invoice Value']=str(details.b2b_inv_val)+' Missmatch'
            # add err to local list of this record
            details_errors.append(err)
        # add list of error to the global 
        # global list contains list of dictionary the key of dictionary contains the id of the
        # record so when you raise error you know with record have erro you can change
        # id by name this is good so you can read the message.
        result.append({'gstr_file id: %s' % gstr_file.id: details_errors})

    if result: # i think you need to check this because you are raising the error no matter what
        # you put raise out of the first loop
        raise UserError(_(str(result)))

如果您正在验证details,只需更改加注,将其置于第一个循环中,但更改列表 到details_errors并且根本不使用结果。

 if details_errors: 
        # you put raise out of the first loop
        raise UserError(_(str(details_errors)))