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

Hello

Please I want to add a function in the report code so I can call it when the report is generated, it works for simple functions that returns simple 'char', but when I put this code it didn't work, for example I want to create some data in the  table 'expense.virement': 

class payment(report_sxw.rml_parse):

     def __init__(self, cr, uid, name, context):
        super(payment, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({'time': time, 'convert':self.convert, })

    def convert(self, amount, cur):
        vir = self.pool.get("expense.virement")
        data = {'col1' : 123, 'col2' : "33", 'col3' : '1223478', 'col4' : '1234' }
        vir.create(cr, uid, data)
        return 'Ok'

report_sxw.report_sxw('report.expense.payment', 'expense.ram', 'addons/expense_ram/report/payment.rml',parser=payment)

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

You cannot use the database cursor (i.e. cr) nor the user id (i.e. uid) in the definition of the function convert.
You have to add them to the localcontext first. 

Try:

     def __init__(self, cr, uid, name, context):
        super(payment, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({'time': time, 'convert':self.convert, 'cr': cr, 'uid', uid, })

    def convert(self, amount, cur):
        vir = self.pool.get("expense.virement")
        data = {'col1' : 123, 'col2' : "33", 'col3' : '1223478', 'col4' : '1234' }
        vir.create(self.cr, self.uid, data)
        return True

 

Hope it helps!

อวตาร
ละทิ้ง
ผู้เขียน

Thanks, I tried what you said but it didn't work

ผู้เขียน

Thanks, I tried what you said but it didn't work

ผู้เขียน

now it works thank you :)

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

How I can fix that please ?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

In convert method, you have to "return data" to return the data. I think you can return any python object in method for report.
 

อวตาร
ละทิ้ง
ผู้เขียน

Thank you for your reply, I can't understand what do you mean ? why I should return the data ? I just want to insert some data to another table when the report is called

Ok, sorry I missed your point. My common use case is that when report is called the data is already in database. I thought your problem is about returning the data.

Related Posts ตอบกลับ มุมมอง กิจกรรม
0
ต.ค. 24
2176
0
ส.ค. 24
1937
0
ม.ค. 24
2370
1
พ.ย. 23
2143
0
มิ.ย. 23
2510