The error occurs because osv
and fields
are outdated in newer versions of Odoo. The correct import is from odoo import models, fields
, and the class should inherit from models.Model
instead of osv.osv
.
Code Error
codefrom osv import osv, fields
class asset_allocation(osv.osv):
_name = 'asset.allocation'
_description = 'Asset Allocation'
_columns = {
'asset_id': fields.many2one('account.asset.asset', 'Asset', required=True),
'employee_id': fields.many2one('hr.employee', 'Employee', required=True),
'asset_status': fields.date.time('Status', required=True),
'note': fields.text('Note'),
}
asset_allocation()
Error:
- Unresolved import: fields
- Undefined variable from import: osv
Blog Post Explanation:
Understanding Python Module Import Errors in OpenERP: ‘Unresolved import’ and ‘Undefined variable from import’
When creating new modules in OpenERP (Odoo) using Eclipse or other development environments, you may encounter the following errors:
codeUnresolved import: fields
Undefined variable from import: osv
Why Does This Error Occur?
The error typically occurs because OpenERP (Odoo) has updated its structure over time. Earlier versions used the osv
module for object classes and fields
for defining models, but newer versions, particularly Odoo 8.0 and later, have replaced osv
with the models
class and a different way to define fields. This change means your imports and class structure need to be updated to match the newer Odoo versions.
How to Fix It?
Here’s how you can resolve the issue:
- Replace
osv
withmodels.Model
: Odoo now usesmodels.Model
instead ofosv.osv
. - Use the correct fields import: Import
fields
fromodoo
and replace field definitions accordingly. - Update your fields syntax: The new syntax for fields like
Many2one
,Char
, andText
has also changed slightly.
Corrected Code:
Here’s how you can update the code to resolve the error:
codefrom odoo import models, fields
class AssetAllocation(models.Model):
_name = 'asset.allocation'
_description = 'Asset Allocation'
asset_id = fields.Many2one('account.asset.asset', string='Asset', required=True)
employee_id = fields.Many2one('hr.employee', string='Employee', required=True)
asset_status = fields.Datetime(string='Status', required=True)
note = fields.Text(string='Note')
# Register the model with Odoo
Key Changes:
- Import the correct module: Use
from odoo import models, fields
instead offrom osv import osv, fields
. - Class inheritance: Instead of inheriting from
osv.osv
, the class now inherits frommodels.Model
. - Field definitions: Replace old field definitions like
fields.many2one
withfields.Many2one
,fields.text
withfields.Text
, etc. - Field parameters: In Odoo, you pass the field label as
string
instead of directly after the field type.
Conclusion:
When upgrading from older versions of OpenERP (Odoo) to newer ones, it’s important to adapt to the updated API and structure. If you’re encountering unresolved import errors, the likely cause is that you’re using legacy code. The key to fixing this is updating the imports, inheritance, and field definitions to match the newer Odoo model architecture. By doing so, you’ll avoid errors and ensure compatibility with the latest version of Odoo.