python - 如何在 Odoo 中返回两个变量?

标签 python python-2.7 odoo odoo-10 odoo-view

我需要检查两个条件并将详细信息打印在报告上。但问题是我无法返回这两个变量。我将提交代码并在下面提及更多内容。

类 TaxDetailReport(models.TransientModel): _name = 'tax.detail.report'

start_date = fields.Datetime(required=True)
end_date = fields.Datetime(required=True)
vat_oman_id = fields.Many2one('vat.oman.import', string="VAT Oman ID")

@api.multi
def generate_report(self):
    for file in self:
        if file.start_date and file.end_date:
            record_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
                                                             ('date', '<=', self.end_date),
                                                             ('account_tax_id.type_tax_use', '=', 'sale')
                                                             ])
            purchase_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
                                                             ('date', '<=', self.end_date),
                                                             ('account_tax_id.type_tax_use', '=', 'purchase')
                                                             ])
        else:
            raise UserError("Record does not exist")

        result['file'] = {'print': [(record_ids,purchase_ids)]}
    return self.env["report"].get_action(result, 'kg_oman_vat.report_tax_details')

我需要返回那些 product_idsrecord_idsgenerate_report 是向导中的按钮。

class VATOmanImport(models.Model):
    _name = 'vat.oman.import'
    _rec_name = 'partner_id'
    _description = 'Oman VAT Import'

    partner_id = fields.Many2one('res.partner', string="Name", required=True)
    invoice_desc = fields.Char(string="Description", help="Invoice Description")
    date = fields.Date(string="Date")
    account_tax_id = fields.Many2one('account.tax', string="Tax Type")
    state_id = fields.Many2one('res.country.state', string="State", required=True,
                               domain="[('country_id', '=','Oman')]")
    invoice_amount = fields.Float(string="Invoice Amount", required=True)
    tax_amount = fields.Float(string="Total Tax", compute='_compute_tax_amount')
    company_id = fields.Many2one('res.company', string='Company', index=True,
                                 default=lambda self: self.env.user.company_id)

上面刚刚提到的是主类,需要从这里获取详细信息。

有什么解决办法吗?希望有人帮忙。

最佳答案

据我了解,您希望在报告中显示这些数据。

因此您需要了解报告中对您有帮助的内容。

您可以像在Python代码中一样调用t-esct-set中的方法。

假设我想在报告中显示一个复杂的值,那么我要做的是:

我创建了一个方法,仅计算并返回我想要打印的值。

  @api.multi
  def calculate_complicated_value(self):
       .....
       .....
       .....
       return value

在我的模板中,我可以调用此方法并打印

  <t t-foreach="docs" t-as="rec">

      <!-- I prefer always to save it in a variable first -->
      <t t-set='result'  t-value='rec.calculate_complicated_value()'/>
      <!-- And here I can loop on my result or handle anything the method call returns -->

我更喜欢这种技术,而不是像 Odoo 开发人员在标准模块中那样在我的 get_action 调用上传递数据。

您可以了解如何将数据传递到报告模板并向他们展示您需要创建额外的 AbstractModel 并且名称必须以 report 开头。

根据您的情况,您可以尝试以下解决方案:

     _name = 'tax.detail.report'

     @api.multi
     def generate_report(self):
                                 # in report docs = self
     return return self.env["report"].get_action(self, 'kg_oman_vat.report_tax_details')


     @api.multi
     def compute_purschaces(self):
            # i don't think you need to loop here because you are calling
            # the method with one record in the report
            # you can refactor it later if you want
            for file in self:
                if file.start_date and file.end_date:
                    record_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
                                                                     ('date', '<=', self.end_date),
                                                                     ('account_tax_id.type_tax_use', '=', 'sale')
                                                                     ])
                    purchase_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
                                                                     ('date', '<=', self.end_date),
                                                                     ('account_tax_id.type_tax_use', '=', 'purchase')
                                                                     ])
                    return {'record_ids': record_ids, 'purchase_ids': purchase_ids}
                else:
                    # raising error from report calls is not a good thing the message will look ugly ^^
                    # try to add this check in generate_report so the message look nice for your user
                    raise UserError("Record does not exist")
            return False    

在您的模板中

           <t t-foreach="docs" t-as="rec">

               <t t-set="result" t-value="rec.compute_purschaces()"/>

               <!-- now if you want to show record_ids in table or something you can access like this result['record_ids'] -->
               <t t-foreach="result['record_ids']" t-as="record">
                    ........
                    .......

在您的报告操作中,模型应为:'tax.detail.report'

  <report
    ...
    ...
    model="tax.detail.report"
   .....
   .....
   ./>

这就是我的做法,它比将额外参数 data 传递给 get_action 调用并创建特殊的 AbstractModel 更容易 在数据进入模板之前对其进行处理,以确保文档设置正确等等。 希望你明白这个想法

关于python - 如何在 Odoo 中返回两个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53172234/

相关文章:

python - 从下面的代码中数据输出是水平的,是否可以垂直

python - 从 panda 数据框中的 ufloat 中提取名义偏差和标准偏差

unit-testing - ODOO [V8] 单元测试

python - Odoo View 错误 : No default view of type ' form' could be found

python - 来自 Python 应用程序的 ElasticSearch/Kibana 中的地理点数据

python - 如何告诉 Matplotlib 创建第二个(新)图,然后在旧图上绘制?

Python:在 subprocess.Popen(..) 中导出环境变量

python-2.7 - HTTP 错误 : HTTP Error 401: Unauthorized for sendgrid integration with python

python - mouseDoubleClickEvent 与 QLineEdit

python - OpenERP自己的前缀序列