Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
18 Trả lời
19893 Lượt xem

When I run this query on Postgresql :

SELECT code, total FROM hr_payslip_line WHERE slip_id=1

I get :

____________________________
|  code    |    total      |
__________________________
|  BASE    |    57000.00   |
____________________________

And when I run this code :

    sql = '''
        SELECT code, total FROM hr_payslip_line WHERE slip_id=%s
    ''' % (slip.id)
    cr.execute(sql)
    for code, total in cr.fetchall() :                
        raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s\nSlip ID : %s\nSQL : %s ' % (code, total,slip.id,sql)))

I've used the raise to catch the error I get this dialog :

    Info

code : BASE
Total : None
Slip ID : 1
SQL : SELECT code, total FROM hr_payslip_line WHERE slip_id=1

The problem is that the total return None and not 57000.00 More informations : 1. I've one slip 2. I've one line 3. the field total is a stored function

Ảnh đại diện
Huỷ bỏ

Can you add slip.id to your debug message and post the result?

Tác giả

The debug is updated, I have used also res = cr.fetchall() and setting res[0] get 'BASE' and res[1] get False

Câu trả lời hay nhất

that's because your code is wrong :-) you should have used

for row in cr.fetchall() :                
        raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (row[0], row[1])))

you can also use

    for row in cr.dictfetchall() :                
            raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (row['code'], row['total'])))

or you can (could?) also use the browse record instead of using an SQL query that ignore the orm (and the access rights checking for example): try with the following:

for line in slip.line_ids:
    raise osv.except_osv(_('Info'),_('code : %s\nTotal :  %s ' % (line.code, line.total)))

if, even with that you cannot get a value for line.total, it means that the total line is not yet computed. To force the computation, just manually call _calculate_total().

Ảnh đại diện
Huỷ bỏ
Tác giả

I replaced the code by the last one I get the same thing code='BASE' and Total=None,

na... you should have done something wrong. Print the row before the raise please

Tác giả

for row in cr.fetchall() : raise osv.except_osv(_('Info'),_('Row : %s' % (row,))) I get : Info

Row : (u'BASE', None) I guess that the problem is the type of total in the orm because it's a field calcultaed but it's stored !!

i don't get the reason, this kind of code is widely used in OpenERP and without any problem... but you can still use the browse record to access the total -_- I updated my answer with a code avoiding the SQL query...

Tác giả

It's my first thing that I have done, using browse get the same thing, when i print line.total I get None so I switch to SQL as solution but not work, it's a very complicate case, but if i find the problem I will publish it, I'm already advanced In OpenObject, and it's first time that I face som

Tác giả

calling the function _calculate_total() has resolved the problem.

Câu trả lời hay nhất

Your code is correct, there are two possibilities:

  • You changed the value of total before or after the code you shown. As you used raise, the transactions are rollbacked and, when you perform your manual query in SQL, you do not see the changed value. Do a cr.commit() just before raise, you may get NULL in your manual SQL query after calling the python code.
  • Both tests are not on the same database.
Ảnh đại diện
Huỷ bỏ
Tác giả

Of course the moment when the query is executed the value of total is False, there is an interference, but how can I get the same thing as the sql request, I use just one database, and I puted cr.commit() before the sql execution, and always same thing, I can share with you all the file

Câu trả lời hay nhất

For me works this way:

  1. sql = "..."

  2. result = self.env.cr.execute(sql)

  3. value = ''

  4. for res in self.env.cr.dictfetchall():

  5.        if (value != ''):

  6.             value += ','

  7.         value += res['number']


  8. record.invoices = value

Hope it helps

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

cr.execute('''SELECT code, total FROM hr_payslip_line WHERE slip_id=%s''' % (slip.id)) x = cr.fetchone() #Only if it returns one (1) row

raise osv.except_osv('Info', 'code : %s\nTotal : %s' % (x[0], x[1]))

#or to return just one row SELECT code, SUM(total) FROM hr_payslip_line WHERE slip_id=1 GROUP BY code

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

I guess that the problem is an interference between the function compute_sheet in the hr_payroll module and my function

I have a stored fucntion field that get total from what shown above, but I put store=False it works well, for me it's not logic and I declared this as a bug,

There is somthing that not good in this module, as a advanced developper I get always values of stored function in the database using SQL and anawhere, this time it's different for me, If I Try to use cr.commit(), there isn't any changes, if I use cr.rollback() I get an other message that say that a field quantity from the object hr_payslip_line doesn't exist, for my module there is not any treatement of field quantity

So if you have a logic to resolve this problem, I'm gratfull for you

To recap : I want to compute a stored function field, I execute the code above and it don't work

Ảnh đại diện
Huỷ bỏ

just manually call _calculate_total()

Tác giả

Ohh Quentin you're found a path , Thank you now I see 57000.00, I have to set this resolved

ok let me add this in my answer, then you can check my answer as correct ^^

Bài viết liên quan Trả lời Lượt xem Hoạt động
4
thg 11 16
2932
1
thg 3 15
3516
2
thg 3 15
5267
3
thg 5 21
7171
0
thg 3 15
3147