73704

Call function from another class - field - Odoo v8

Question:

I'm struggling with this and I'm not so clear about it.

Let's say I have a function in a class:

class my_class(osv.Model): _name = 'my_class' _description = 'my description' def func (self, cr, uid, ids, name, arg, context=None): res = dict((id, 0) for id in ids) sur_res_obj = self.pool.get('contratos.user_input') for id in ids: res[id] = sur_res_obj.search(cr, uid, # SUPERUSER_ID, [('contratos_id', '=', id), ('state', '=', 'done')], context=context, count=True) return res columns = { 'function': fields.function(_get_func, string="Number of completed Contratos", type="integer"), my_class()

Now I want to call this very same function from another class-object:

class another_class(osv.Model): _inherit = 'my_class' _name = 'my_class' columns = { 'another_field' : fields.related('function', 'state', type='char', string = "Related field which calls a function from another object"), }

But this isn't working, I'm very confused about this, how can I call a function from another object in Odoov8?

I've heard about self.pool.get but I'm not really sure on how to invoke it.

Any ideas?

Thanks in advance!

Answer1:

Since you're using odoo8 you should use the new API. from the docs

<blockquote>

In the new API the notion of Environment is introduced. Its main objective is to provide an encapsulation around cursor, user_id, model, and context, Recordset and caches

</blockquote> def my_func(self): other_object = self.env['another_class'] other_object.create(vals) # just using create as an example

That means you don't need to explicity pass cr, uid, ids, vals and context in your methods anymore and you don't use self.pool.get() even though it's still there for backward compatibility

env is dictionary-like object that is used to store instances of the Odoo models and other information so you can access other objects and their methods from any Odoo model.

Answer2:

def example(self, cr, uid, ids, context=None): otherClass = self.pool.get('my_class') ... otherClass.func(cr, uid, otherClassIds, name, arg, context)

<a href="http://abhishek-jaiswal.github.io/blog/odoov8/2014/11/15/odoo-v8-decorator.html" rel="nofollow">More information.</a>

Recommend

  • Avoiding dependencies is exploding the number of projects in my VS solution
  • mysql and indexes with more than one column
  • Where in the relevant specification is it documented that some comments in a SQL script are, in fact
  • Reloading table causes flickering
  • How to add a filter to Active Admin dashboard?
  • Open hyperlink on click on an ggplot/plotly chart
  • Getting IIS6 to play nice with WordPress Pretty Permalinks
  • Accessing local repository in offline mode
  • MarkLogic Node.js Sort on “last-modified”
  • c++ search a vector for element first seen position
  • getelement by class name for clicking
  • Using android opencv apps without downloading opencv sdk manager
  • Efficient User-Agent Regex to find Safari in Python
  • Django Haystack Rebuild Index
  • jQuery: How to AJAXify WordPress Search?
  • Primefaces :radioButton inside a ui:repeat
  • How to install node-mysql?
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Selenium to click on a javascript button corresponding to a text
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • Google Custom Search with transparent background
  • How can I send an e-mail from a vbs script
  • Insert into database using onclick function
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • What is Eclipse's Declaration View used for?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Can I make an Android app that runs a web view in Chrome 39?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • how does django model after text[] in postgresql [duplicate]
  • apache spark aggregate function using min value
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?