Skip to Content
Odoo เมนู
  • ลงชื่อเข้าใช้
  • ทดลองใช้ฟรี
  • แอป
    การเงิน
    • ระบบบัญชี
    • ระบบการออกใบแจ้งหนี้
    • ระบบบัญชีรายจ่าย
    • ระบบการจัดการข้อมูลผ่านสเปรดชีต (BI)
    • เอกสาร
    • ระบบลงลายเซ็น
    การขาย
    • ลูกค้าสัมพันธ์
    • การขาย
    • POS ร้านค้า
    • POS ร้านอาหาร
    • ระบบสมัครสมาชิก
    • การเช่า
    การจัดการเว็บไซต์
    • เครื่องมือสร้างเว็บไซต์
    • ระบบการขายออนไลน์
    • บล็อก
    • ฟอรั่ม
    • ไลฟ์แชท
    • ระบบการอบรมออนไลน์
    ซัพพลายเชน
    • คลังสินค้า
    • ระบบการผลิต
    • PLM
    • ระบบการจัดซื้อ
    • การซ่อมบำรุง
    • ระบบจัดการคุณภาพ
    ทรัพยากรบุคคล
    • ข้อมูลพนักงาน
    • ระบบสรรหาบุคลากร
    • ระบบการลา
    • การประเมิน
    • ระบบการแนะนำพนักงาน
    • ระบบจัดการยานพาหนะ
    การตลาด
    • โซเชียลมาร์เก็ตติ้ง
    • การตลาดผ่านอีเมล
    • ระบบการตลาดผ่าน SMS
    • ระบบการจัดงานอีเวนต์
    • การตลาดแบบอัตโนมัติ
    • ระบบแบบสำรวจ
    การบริการ
    • โปรเจกต์
    • ระบบบันทึกเวลา
    • ระบบการจัดการบริการภาคสนาม
    • ระบบดูแลช่วยเหลือ
    • การวางแผน
    • การนัดหมาย
    ประสิทธิผล
    • ระบบแสดงความคิดเห็น
    • ระบบการอนุมัติ
    • ไอโอที
    • VoIP
    • คลังข้อมูล
    • WhatsApp
    แอปพลิเคชันของบุคคลที่สาม Odoo สตูดิโอ แพลตฟอร์มคลาวด์ของ Odoo
  • อุตสาหกรรม
    การค้าปลีก
    • ร้านขายหนังสือ
    • ร้านขายเสื้อผ้า
    • ร้านขายเฟอร์นิเจอร์
    • ร้านขายของชำ
    • ร้านขายฮาร์ดแวร์
    • ร้านขายของเล่น
    Food & Hospitality
    • บาร์และผับ
    • ร้านอาหาร
    • อาหารฟาสต์ฟู้ด
    • Guest House
    • ตัวแทนจำหน่ายเครื่องดื่ม
    • Hotel
    อสังหาริมทรัพย์
    • Real Estate Agency
    • บริษัทสถาปัตยกรรม
    • บริษัทรับเหมา
    • การจัดการอสังหาริมทรัพย์
    • การจัดสวน
    • สมาคมเจ้าของอสังหาริมทรัพย์
    ปรึกษา
    • สำนักงานบัญชี
    • Odoo พาร์ทเนอร์
    • เอเจนซี่การตลาด
    • สำนักงานกฎหมาย
    • บริการสรรหาบุคลากร
    • การตรวจสอบและการรับรอง
    ระบบการผลิต
    • Textile
    • เหล็ก
    • เฟอร์นิเจอร์
    • อาหาร
    • Brewery
    • ของขวัญขององค์กร
    สุขภาพ & ฟิตเนส
    • ชมรมกีฬา
    • ร้านขายแว่นตา
    • ฟิตเนส
    • ผู้ประกอบวิชาชีพด้านสุขภาพ
    • ร้านขายยา
    • ร้านทำผม
    Trades
    • ช่างซ่อม
    • ฮาร์ดแวร์และการสนับสนุนด้านไอที
    • ระบบพลังงานแสงอาทิตย์
    • ช่างทำรองเท้า
    • บริการทำความสะอาด
    • HVAC Services
    อื่น ๆ
    • Nonprofit Organization
    • สำนักงานสิ่งแวดล้อม
    • ร้านเช่าป้ายโฆษณา
    • การถ่ายภาพ
    • ร้านเช่าจักรยาน
    • ตัวแทนจำหน่ายซอฟต์แวร์
    Browse all Industries
  • คอมมูนิตี้
    ศึกษา
    • บทเรียน
    • เอกสารกำกับโปรแกรม
    • การรับรอง
    • การฝึกอบรม
    • บล็อก
    • พอดแคสต์
    เพิ่มศักยภาพให้กับการศึกษา
    • โปรแกรมการศึกษา
    • Scale Up! Business Game
    • เยี่ยมชม Odoo
    รับซอฟต์แวร์
    • ดาวน์โหลด
    • เปรียบเทียบรุ่น
    • เผยแพร่
    ทำงานร่วมกัน
    • Github
    • ฟอรั่ม
    • ระบบการจัดงานอีเวนต์
    • การแปล
    • ร่วมเป็นพาร์ทเนอร์
    • Services for Partners
    • ลงทะเบียนเพื่อสร้างบัญชีบริษัทของคุณ
    รับบริการ
    • ค้นหาพาร์ทเนอร์
    • ค้นหานักบัญชี
    • พบกับที่ปรึกษา
    • บริการติดตั้งระบบเพื่อใช้งาน
    • ข้อมูลอ้างอิงลูกค้า
    • การสนับสนุน
    • อัปเกรด
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    รับการสาธิต
  • ราคา
  • ช่วยเหลือ

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • ลูกค้าสัมพันธ์
  • e-Commerce
  • ระบบบัญชี
  • สินค้าคงคลัง
  • PoS
  • โปรเจกต์
  • MRP
All apps
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
All Posts คน เหรียญรางวัล
แท็ก (View all)
odoo accounting v14 pos v15
เกี่ยวกับฟอรั่มนี้
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
All Posts คน เหรียญรางวัล
แท็ก (View all)
odoo accounting v14 pos v15
เกี่ยวกับฟอรั่มนี้
ช่วยเหลือ

Can we import a custom function from one of the custom modules, not from odoo...?

ติดตาม

รับการแจ้งเตือนเมื่อมีกิจกรรมในโพสต์นี้

คำถามนี้ถูกตั้งค่าสถานะ
functionimportcustomize
4 ตอบกลับ
3105 มุมมอง
อวตาร
noone

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 ?


1
อวตาร
ละทิ้ง
อวตาร
Savya Sachin
คำตอบที่ดีที่สุด

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

1
อวตาร
ละทิ้ง
noone
ผู้เขียน

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?

Yahoo Baba Innovations Pvt.Ltd

Hello noone,

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

Savya Sachin

I have updated the answer for your case.

อวตาร
Alex Dhika
คำตอบที่ดีที่สุด

Add all module to depends section in your manifest file

0
อวตาร
ละทิ้ง
Yahoo Baba Innovations Pvt.Ltd

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!

อวตาร
noone
ผู้เขียน คำตอบที่ดีที่สุด

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?

0
อวตาร
ละทิ้ง
อวตาร
Yahoo Baba Innovations Pvt.Ltd
คำตอบที่ดีที่สุด

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

0
อวตาร
ละทิ้ง
noone
ผู้เขียน

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.

Yahoo Baba Innovations Pvt.Ltd

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']

noone
ผู้เขียน

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 ตอบกลับ มุมมอง กิจกรรม
How to override IMPORT button on tree view using Odoo 12
import customize button odoo12
อวตาร
0
พ.ค. 20
3300
Issue with import and comma in Excel File (odoo 19)
import
อวตาร
0
ก.ย. 25
234
production of not enough materials for a PO
function
อวตาร
อวตาร
อวตาร
2
ก.ย. 25
1113
POS Product Category Community Edition
function
อวตาร
อวตาร
1
ก.ย. 25
1786
Options for collections management addons for museums or private collection
function
อวตาร
0
ก.ค. 25
1175
คอมมูนิตี้
  • บทเรียน
  • เอกสารกำกับโปรแกรม
  • ฟอรั่ม
โอเพนซอร์ส
  • ดาวน์โหลด
  • Github
  • Runbot
  • การแปล
บริการ
  • โฮสติ้ง odoo.sh เซิร์ฟเวอร์
  • การสนับสนุน
  • อัปเกรด
  • การพัฒนาที่กำหนดเอง
  • การศึกษา
  • ค้นหานักบัญชี
  • ค้นหาพาร์ทเนอร์
  • ร่วมเป็นพาร์ทเนอร์
เกี่ยวกับเรา
  • บริษัทของเรา
  • ทรัพย์สินในรูปของเครื่องหมายการค้า
  • ติดต่อเรา
  • งาน
  • ระบบการจัดงานอีเวนต์
  • พอดแคสต์
  • บล็อก
  • ลูกค้า
  • กฎหมาย • ความเป็นส่วนตัว
  • ความปลอดภัย
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo เป็นชุดแอปธุรกิจแบบเปิดที่ครอบคลุมทุกความต้องการของบริษัทของคุณ อาธิ CRM eCommerce ระบบบัญชี ระบบสินค้าคงคลัง ระบบการขายหน้าร้าน โปรเจกต์ และ อีกมากมาย

ความเหนือกว่าอันเป็นเอกลักษณ์ของ Odoo คือการใช้งานที่ง่ายดายและผสานการทำงานอย่างสมบูรณ์แบบในเวลาเดียวกัน

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now