Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
9942 มุมมอง

In a custom module, if you want to define model FOOBAR  you just write the code in foobar.py and include

from . import foobar 

in __init__.py

So far, so good.

I want to move my foobar.py to folder models. What shoud be included in __init__.py to reflect the fact that foobar.py is in "models" folder?

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

your models need to be a python package so it needs a __init__.py with the foobar.py import inside, and in the module root you import the models package. this is the structure:

module_root

----models

--------__init__.py               #import foobar

--------foobar.py
----__init__.py                    #import models
----__openerp__.py

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

Thanks, it makes sense once explained

คำตอบที่ดีที่สุด

This has been working for me in v10 and v11 but now in v12 is not working and i am looking for ideas. Exact same case.


My module files are listed  like folows:

-module folder

-- __init__.py

from . import models

--models

--- __init__.py

from . import myfile

---myfile.py


อวตาร
ละทิ้ง

In Odoo newer versions there are some small changes, like:

__openerp__.py was renamed into __manifest__.py

Due to the usage of Python 3 some imports need to be done against the local package like:

from . import models

from . import foobar

Otherwise Python 3 will try to look for an installed library

Related Posts ตอบกลับ มุมมอง กิจกรรม
0
ต.ค. 19
4175
Can't import a model in odoo view แก้ไขแล้ว
7
ก.ค. 19
41573
2
ม.ค. 25
966
1
ก.พ. 23
7192
2
ส.ค. 18
11483