Skip to Content
Menu
This question has been flagged
4 Replies
1072 Views

Hello guys:


I'm developing in inherit/extension way. 

Some self-made functions are common I want share and reuse in my custom modules, I tried to put them in a module and import them in others,  so I do not need to repeat them everywhere. 

But seems it's not supported ,  an error raised as below:


The directories' structure like this:

Can we import functions from self-made modules?  or we can ONLY import from odoo source directories ?


Avatar
Discard
Best Answer

Hi, 

You can import Odoo functions from self-made modules and from Odoo source directories. following are a few scenarios with examples.

Importing Odoo Functions in Custom Modules

1. Importing from Odoo source directories

from odoo import models, fields, api

from odoo.exceptions import ValidationError


2. Importing from your own custom modules

# Assuming you have a custom module named 'my_module' with a file 'helpers.py'

from . import helpers

from .models import my_model


3. Relative imports within your module

# In a file within your module, e.g., 'models/product.py'

from . import common

from ..helpers import utils


4. Importing specific functions or classes

from odoo.addons.base.models.res_partner import Partner


5. Using imported functions

class CustomModel(models.Model):

    _name = 'custom.model'


    @api.model

    def custom_method(self):

        #Using a function from Odoo core

        self.env['res.partner'].create({'name': 'New Partner'})

       

        #Using a function from your custom helper

        result = helpers.custom_calculation()

       

        #Using a model from another custom module

        my_model.do_something()


6. Importing and using external libraries

#Make sure the library is installed in your Odoo environment

import requests


def external_api_call(self):

    response = requests.get('https://api.example.com/data')

    # Process the response...


In your case, Try this way to import,

from odoo.addons.my_module1.your_python_file_name import my_method


Note: When importing external libraries, ensure they're listed in your module's external dependencies.

Thanks

Avatar
Discard
Author

Hi Savya:

Thanks a lot for your wide-ranging answer! My case is more like the scenario #4, importing across modules but NOT odoo source modules.
Please refer to the lower picture i attached, I defined a method in one of my customized module, and I also need it in other customized modules. So the importing path like ‘ odoo.addons.base... ’ is not applicable.

Yes, i know the solution #5 using "self.env['model_name']. method_name" and it works indeed,
but I'm wondering any chance in 'import' way?

Hello noone,

You just import below line :
from odoo import fields, models, api

I have updated the answer for your case.

Best Answer

Add all module to depends section in your manifest file

Avatar
Discard

Hii noone,

Use this line and add you custom logic. This is just an example.

from odoo.addons.account.tools import format_rf_reference

from odoo.addons.account.tools import format_rf_reference

class CustomModule(models.Model):
_name = 'custom.module'

reference_number = fields.Char('Reference Number')
formatted_reference = fields.Char('Formatted Reference', compute='_compute_formatted_reference')

@api.depends('reference_number')
def _compute_formatted_reference(self):
for record in self:
if record.reference_number:
# Use Odoo's format_rf_reference function for formatting
record.formatted_reference = format_rf_reference(record.reference_number)
else:
record.formatted_reference = False

Let me know if you need further clarification!

Author Best Answer

I believe most of us must have some functions which are  converged、common and to be used in some other modules. That's what modularity is all about. 

Example #1 :

we know the function 'float_compare()' in base module,  which comparing 2 floats' value.  It's just a function,  not belongs to any model . And it can be used everywhere you need by simply importing it :


Example #2 :

Even in business modules, there also have some shared functions defined in 'tools'  and to be imported in other places. 

In use: 


But similar mechanism does not work in custom modules. I defined a function to compute sequence,  and want to use it in business module (such as employee、partner、products、orders etc.) .  But importing raises error.


Of course I know there're 2 ways :

#1 、 Just repeating the code where I need it ;

#2、 Using "self.env['source_model'].get_sequence " to call it ;

But seems both above are not the reasonable solution,  efficiency or readability is not good. 

I just wondering if and how we can import such custom function from one custom module into another?

Avatar
Discard
Best Answer

Hello noone,

If you are confused please share image what you do or you can connect with me i'll guide where you mistake.

Skype : Yahoo Baba Official

Email:  yahoobaba077@gmail.com

Avatar
Discard
Author

Thanks friend ! I've update the image of file structure above.
In short : how to import a custom function from one custom module to another ? Not odoo's function in source / original directories.

Hii noone,

You need to add this type of function in your local function and add another module name in manifest file in depends.

To add a custom class and use the onchange_custom_function, you can define your custom model class and override the method. Here's the reformatted code including the custom class:

python :
from odoo import models, fields

class CustomModel(models.Model):
_inherit = 'another.module' # Inherit from the appropriate one module.

custom_field = fields.Char('Custom Field')

def onchange_custom_function(self):
rec = super(CustomModel, self).onchange_custom_function()
# Add your custom logic here
return rec
Make sure to include the module's name in the depends section of the manifest file (__manifest__.py), like so:

python copy code:
'depends': ['base_module', 'your_custom_module']

Author

Thank you very much my friend! Your solution looks like inheriting an exsiting model and override its method.
I meant an indiviual、customized method itself, defined in one custom module and can be utilized in other custom modules.
Let me explain more detail afterwards.

Related Posts Replies Views Activity
0
May 20
2372
0
Apr 25
66
2
Apr 25
148
0
Mar 25
319
0
Mar 25
303