Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Call python object

By
Yves Goldberg
on 11/25/16, 5:50 AM 125 views

I use a python library in the form:

class SomeClass(object) 
    def __init__(self, arg1, arg2): ...     def fct1(self):
        ...    return something

Directly in python:

x = SomeClass(arg1,arg2)

create an object alright.

I would like to do the same in Odoo. I tried the following:

class SomeClass(models.Model)
  def connect(self, arg1, arg2):
...
def fct1(self):
...
return something

But 

x = connect(arg1,arg2)

returns :

NameError: global name 'connect' is not defined

How should I use my  python library?

TIA

0

zbik

--zbik--
5976
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 11/25/16, 7:47 AM

Try this:

x = self.connect(arg1,arg2)

when you call in SomeClass or (when SomeClass is instance)

x = SomeClass.connect(arg1,arg2)


Example:

class MyClass(models.Model):
    _name = 'my.class'
    def _test(self,a,b):
        return a+b

class OtherClass(models.Model):
...
    MyClass = self.env['my.class'] # create instance
    result = MyClass._test(2,3)
...
print result
> 5


Example 2:  myclass.py in folder myaddons

class MyClass:
def __init__(self, name):
self.name = name
def _test(self,a,b):
return a+b

in other Odoo class:

from openerp.addons.myaddons.myclass import MyClass
...
x = MyClass('Hello')
y = x._test(2,3)
...
print x.name
> Hello
print y
> 5




 

I tried it but it return "None" when I "print x". an instance is not created

Ygol InternetWork, Yves Goldberg
on 11/25/16, 8:06 AM

Answer updated with instance creation example.

zbik
on 11/25/16, 9:13 AM

I am aware that I have to use self.env['my.class'] to access a method in a separate class in odoo. What I try to do is using python code that is not in odoo. How would I call a method that is under a class 'SomeClass(object)' having a __init__ constructor function (vs. a classic odoo model that is using 'models.Model' and do not have the __init__ function)?

Ygol InternetWork, Yves Goldberg
on 11/25/16, 10:15 AM

Example 2

zbik
on 11/25/16, 12:52 PM

It works fine. Thank you a bunch for your help zbik!!

I thought that by declaring the code in the __init__.py code of that directory (models):

> import myclass

I could use it but it seem it is not the case and when importing it in the Odoo Class, the methods are available to be called.

Ygol InternetWork, Yves Goldberg
on 11/26/16, 3:09 AM
0
Yves Goldberg
On 11/26/16, 3:06 AM

It works fine. Thank you a bunch for your help zbik!!

I thought that by declaring the code in the __init__.py code of that directory (models):


import myclass

I could use it but it seem it is not the case and when importing it directly in the Odoo Class; the methods are available to be called.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/25/16, 5:50 AM
Seen: 125 times
Last updated: 11/26/16, 3:07 AM