2016-02-16 98 views
1

我正在嘗試創建一個模塊來管理員工的旅行離開。Odoo - 如何從另一個模塊更新字段值

我需要做的是在'available_tickets'的hr.employee模塊中添加額外的float字段。從其他名爲「travel_request」的模塊分配。這個怎麼做? 這裏是我的代碼:

class travel_request(osv.osv): 

    _name = 'travel.request' 

    _columns = { 

     'no_tickets': fields.float('Number of Tickets'), 

    } 

class hr_employee(osv.osv): 
_inherit="hr.employee" 


def _get_tickets(self, cr, uid, ids, field_name, args, context=None): 
    res = {} 
    tickets_obj = self.pool.get('travel.request') 
    tickets_ids = self.pool.get('travel.request').search(cr, uid, [('user_id', '=', uid), ('state', '=', 'validate')]) 
    for t_id in tickets_ids : 
     emp_ticket = tickets_obj.browse(cr, uid, t_id, context=context) 

    res['available_tickets'] = emp_ticket.no_tickets 
    return res  


_columns = { 
    'available_tickets': fields.function(_get_tickets, string='Available Tickets', type="float", store=True), 
} 

我也試過這個方法:

def _get_tickets(self, cr, uid, ids, field_name, args, context=None): 
    res = {} 
    tickets_obj = self.pool.get('travel.request') 
    for record in self.browse(cr, uid, ids, context=None): 
     tickets_ids = tickets_obj.search(cr, uid, [('user_id', '=', uid), ('state', '=', 'validate')], ids, context=None) 
     emp_ticket = tickets_obj.browse(cr, uid, tickets_ids[0], context=None) 
     res['available_tickets'] = emp_ticket.no_tickets   
    return res 

但是什麼都沒有發生,我不知道什麼是錯的..任何人都可以幫我解決這個問題

這裏的錯誤消息:

2016-02-16 13:29:14,549 27608 ERROR test2_travel openerp.http: Exception during JSON request handling. 
Traceback (most recent call last): 
    File "/opt/odoo8/odoo/openerp/http.py", line 537, in _handle_exception 
    return super(JsonRequest, self)._handle_exception(exception) 
    File "/opt/odoo8/odoo/openerp/http.py", line 574, in dispatch 
    result = self._call_function(**self.params) 
    File "/opt/odoo8/odoo/openerp/http.py", line 310, in _call_function 
    return checked_call(self.db, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/service/model.py", line 118, in wrapper 
    return f(dbname, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/http.py", line 307, in checked_call 
    return self.endpoint(*a, **kw) 
    File "/opt/odoo8/odoo/openerp/http.py", line 803, in __call__ 
    return self.method(*args, **kw) 
    File "/opt/odoo8/odoo/openerp/http.py", line 403, in response_wrap 
    response = f(*args, **kw) 
    File "/opt/odoo8/odoo/addons/web/controllers/main.py", line 944, in call_kw 
    return self._call_kw(model, method, args, kwargs) 
    File "/opt/odoo8/odoo/addons/web/controllers/main.py", line 936, in _call_kw 
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/api.py", line 268, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/addons/hr_holidays/hr_holidays.py", line 494, in create 
    return super(hr_employee, self).create(cr, uid, vals, context=context) 
    File "/opt/odoo8/odoo/openerp/api.py", line 268, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/addons/hr/hr.py", line 285, in create 
    employee_id = super(hr_employee, self).create(cr, uid, data, context=context) 
    File "/opt/odoo8/odoo/openerp/api.py", line 268, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/addons/mail/mail_thread.py", line 381, in create 
    thread_id = super(mail_thread, self).create(cr, uid, values, context=context) 
    File "/opt/odoo8/odoo/openerp/api.py", line 268, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/api.py", line 372, in old_api 
    result = method(recs, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/models.py", line 4094, in create 
    record = self.browse(self._create(old_vals)) 
    File "/opt/odoo8/odoo/openerp/api.py", line 266, in wrapper 
    return new_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/api.py", line 508, in new_api 
    result = method(self._model, cr, uid, *args, **old_kwargs) 
    File "/opt/odoo8/odoo/openerp/models.py", line 4279, in _create 
    result += self._columns[field].set(cr, self, id_new, field, vals[field], user, rel_context) or [] 
    File "/opt/odoo8/odoo/openerp/osv/fields.py", line 1486, in set 
    self._fnct_inv(obj, cr, user, id, name, value, self._fnct_inv_arg, context) 
    File "/opt/odoo8/odoo/addons/hr/hr.py", line 186, in _set_image 
    return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context) 
    File "/opt/odoo8/odoo/openerp/api.py", line 268, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/addons/mail/mail_thread.py", line 432, in write 
    result = super(mail_thread, self).write(cr, uid, ids, values, context=context) 
    File "/opt/odoo8/odoo/openerp/api.py", line 268, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/api.py", line 399, in old_api 
    result = method(recs, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/models.py", line 3789, in write 
    self._write(old_vals) 
    File "/opt/odoo8/odoo/openerp/api.py", line 266, in wrapper 
    return new_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/api.py", line 592, in new_api 
    result = method(self._model, cr, uid, self.ids, *args, **old_kwargs) 
    File "/opt/odoo8/odoo/openerp/models.py", line 4037, in _write 
    cr, user, todo, fields_to_recompute, context) 
    File "/opt/odoo8/odoo/openerp/api.py", line 268, in wrapper 
    return old_api(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/models.py", line 4453, in _store_set_values 
    cr.execute(query, (column._symbol_set[1](value), id)) 
    File "/opt/odoo8/odoo/openerp/sql_db.py", line 158, in wrapper 
    return f(self, *args, **kwargs) 
    File "/opt/odoo8/odoo/openerp/sql_db.py", line 234, in execute 
    res = self._obj.execute(query, params) 
DataError: invalid input syntax for integer: "available_tickets" 
LINE 1: ...ployee" SET "available_tickets"='5.00' WHERE id = 'available... 
+0

您需要更改值*飛行*?我的意思是在GUI中? –

+1

是的,抱歉,我沒有定義該字段作爲功能..我已經更新了 – youta

回答

4

您有多個問題。

1.你的錯誤信息

錯誤消息DataError: invalid input syntax for integer: "available_tickets"點,你的錯誤。您的計算字段實現返回一個dict,其中"available_tickets"作爲關鍵字而不是員工標識。因此,當系統嘗試更新數據庫時,它無法找到ID爲"available_tickets"的員工記錄。

問題出在get_tickets()

res['available_tickets'] = emp_ticket.no_tickets 

您需要遍歷傳遞給get_tickets()員工ids並返回結果res,看起來像這樣:所以

{ 
    1: 3.0, # 3 `available_tickets` for employee ID 1 
    2: 4.0, # 4 `available_tickets` for employee ID 2 
    ... 
} 

get_tickets()你應該有這樣的事情:

res[emp.id] = <computed number of tickets> # emp = hr.employee record! 

請查看用於編寫計算字段(old API,new API)的文檔或官方Odoo模塊中的示例以獲取靈感。

2.您的計算字段邏輯

您當前(草案?)實現的get_tickets()變得毫無意義:

  • 它應該對所請求的hr.employee ID的主循環。這很重要,因爲Odoo計算字段是由系統批量計算的。
  • 它應該可能不依賴於當前用戶來計算結果。目前您正在通過此域('user_id', '=', uid)搜索當前用戶的travel.requests。這意味着您正在查看正在查看或修改員工的用戶的travel.requests,而不是該員工的用戶。這是應該使用ids變量的地方;以browse爲您計算值的員工,然後搜索相應的travel.request
  • 如果您要存儲計算值(store=True),則需要適當的重新計算觸發器,否則當您修改/添加travel.request時,存儲值不會更改。在新的API中,這是通過@api.depends完成的,並且在舊API中通過傳遞store字典來完成,如文檔中所述。

閱讀官方插件的文檔和源代碼實際上是瞭解Odoo計算字段的最佳方式。

+0

親愛的開發者,我完全同意你的看法。您的答案是最正確,最完整和最豐富的。所以我的回答沒有意義。祝你好運!) –

相關問題