Odoo - 初始化函数不工作
Odoo - init function is not working
我在示例模块中工作,我正在创建一个视图,我想在其中显示基于条件和来自多个模型的记录。已经在创建中。
Pending Accounts
class PendingAccounts(models.Model):
_name = 'amgl.pending_accounts'
_description = 'Pending Account'
name = fields.Char()
first_name = fields.Char(string="First Name")
last_name = fields.Char(string="Last Name")
quantity_expected = fields.Float(string="Quantity Expected")
quantity_received = fields.Float(string="Quantity Received")
possible_reason = fields.Many2one('amgl.product_reason',string='Possible Reason')
possible_solution = fields.Many2one('amgl.product_solution', string='Possible Solution')
notes = fields.Html(string='Notes')
@api.model_cr
def init(self):
tools.drop_view_if_exists(self._cr, 'pending_accounts')
self._cr.execute("""
CREATE VIEW pending_accounts AS (
SELECT c.name AS first_name,
c.last_name AS last_name,
o.total_received_qty AS quantity_received,
o.total_qty AS quantity_expected
FROM amgl_order AS o
INNER JOIN amgl_customer AS c ON c.id = o.customer_id
WHERE o.is_pending = True
)""")
Pending Accounts View
<odoo>
<data>
<record id="amgl.pending_accounts_action_window" model="ir.actions.act_window">
<field name="name">Pending Accounts</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">amgl.pending_accounts</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
<!-- Add Text Here -->
</p><p>
<!-- More details about what a user can do with this object will be OK -->
</p>
</field>
</record>
<record id="amgl.pending_accounts_form" model="ir.ui.view">
<field name="name">Pending Accounts Form</field>
<field name="model">amgl.pending_accounts</field>
<field name="arch" type="xml">
<form string="Pending Inventories">
<sheet>
<group>
<field name="first_name"/>
<field name="last_name"/>
<field name="quantity_expected"/>
<field name="quantity_received"/>
<field name="possible_reason"/>
<field name="possible_solution"/>
<field name="notes"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="amgl.pending_accounts_tree" model="ir.ui.view">
<field name="name">Pending Account</field>
<field name="model">amgl.pending_accounts</field>
<field name="arch" type="xml">
<tree string="Pending Accounts">
<field name="first_name"/>
<field name="last_name"/>
<field name="quantity_expected"/>
<field name="quantity_received"/>
<field name="possible_reason"/>
<field name="possible_solution"/>
<field name="notes"/>
</tree>
</field>
</record>
</data>
</odoo>
所以我创建了一个模型并添加了查询以在模型的 init
方法中获取所有这些记录。但它没有在树 view.I 中显示任何记录,也没有在 pgadmin 中的查询 运行 中显示,并且有一条记录针对此查询,如附件所示。
我现在面临以下错误,
Traceback (most recent call last):
File "/home/ahsan/v10/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/ahsan/v10/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/home/ahsan/v10/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/ahsan/v10/odoo/service/model.py", line 101, in wrapper
return f(dbname, *args, **kwargs)
File "/home/ahsan/v10/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/home/ahsan/v10/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/home/ahsan/v10/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/home/ahsan/v10/addons/web/controllers/main.py", line 827, in
search_read
return self.do_search_read(model, fields, offset, limit, domain, sort)
File "/home/ahsan/v10/addons/web/controllers/main.py", line 849, in
do_search_read
offset=offset or 0, limit=limit or False, order=sort or False)
File "/home/ahsan/v10/odoo/models.py", line 4681, in search_read
records = self.search(domain or [], offset=offset, limit=limit, order=order)
File "/home/ahsan/v10/odoo/models.py", line 1518, in search
res = self._search(args, offset=offset, limit=limit, order=order, count=count)
File "/home/ahsan/v10/odoo/models.py", line 4242, in _search
self._cr.execute(query_str, where_clause_params)
File "/home/ahsan/v10/odoo/sql_db.py", line 141, in wrapper
return f(self, *args, **kwargs)
File "/home/ahsan/v10/odoo/sql_db.py", line 218, in execute
res = self._obj.execute(query, params)
ProgrammingError: column amgl_pending_accounts.id does not exist
LINE 1: SELECT "amgl_pending_accounts".id FROM "amgl_pending_account...
使用_auto=False执行init()方法。
是否应该创建数据库table(默认值:True)
如果设置为 False,覆盖 init() 以创建数据库 table。
编辑
在这种情况下,首先我们需要从数据库中永久删除 table。转到您的终端并按照以下命令操作:
sudo su postgres
psql <YourDatabaseName>
DROP TABLE amgl_pending_accounts CASCADE;
\q
现在刷新网络浏览器并升级模块。
如果你定义了 _name = 'amgl.pending.acounts'
试试这个
odoo 预计数据库中 table 的名称是:amgl_pending_accounts
试试这个。只需了解 (.) 将是 table 名称中的 (_)。
class PendingAccounts(models.Model):
_name = 'amgl.pending.accounts'
_description = 'Pending Account'
_auto = False
name = fields.Char()
first_name = fields.Char(string="First Name")
last_name = fields.Char(string="Last Name")
quantity_expected = fields.Float(string="Quantity Expected")
quantity_received = fields.Float(string="Quantity Received")
possible_reason = fields.Many2one('amgl.product_reason',string='Possible Reason')
possible_solution = fields.Many2one('amgl.product_solution', string='Possible Solution')
notes = fields.Html(string='Notes')
@api.model_cr
def init(self):
tools.drop_view_if_exists(self._cr, 'pending_accounts')
self._cr.execute("""
CREATE VIEW amgl_pending_accounts AS (
SELECT
-- by default view expect the id columns to exists.
row_number() OVER () AS id,
c.name AS first_name,
c.last_name AS last_name,
o.total_received_qty AS quantity_received,
o.total_qty AS quantity_expected
FROM amgl_order AS o
INNER JOIN amgl_customer AS c ON c.id = o.customer_id
WHERE o.is_pending = True
)""")
XML:
<odoo>
<data>
<record id="amgl.pending.accounts_action_window" model="ir.actions.act_window">
<field name="name">Pending Accounts</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">amgl.pending.accounts</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
<!-- Add Text Here -->
</p><p>
<!-- More details about what a user can do with this object will be OK -->
</p>
</field>
</record>
<record id="amgl.pending.accounts_form" model="ir.ui.view">
<field name="name">Pending Accounts Form</field>
<field name="model">amgl.pending.accounts</field>
<field name="arch" type="xml">
<form string="Pending Inventories">
<sheet>
<group>
<field name="first_name"/>
<field name="last_name"/>
<field name="quantity_expected"/>
<field name="quantity_received"/>
<field name="possible_reason"/>
<field name="possible_solution"/>
<field name="notes"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="amgl.pending.accounts_tree" model="ir.ui.view">
<field name="name">Pending Account</field>
<field name="model">amgl.pending.accounts</field>
<field name="arch" type="xml">
<tree string="Pending Accounts">
<field name="first_name"/>
<field name="last_name"/>
<field name="quantity_expected"/>
<field name="quantity_received"/>
<field name="possible_reason"/>
<field name="possible_solution"/>
<field name="notes"/>
</tree>
</field>
</record>
</data>
</odoo>
您可以使用以下方法完成。
class PendingAccounts(models.Model):
_name = 'amgl.pending_accounts'
_description = 'Pending Account'
_auto=False
name = fields.Char()
first_name = fields.Char(string="First Name")
last_name = fields.Char(string="Last Name")
quantity_expected = fields.Float(string="Quantity Expected")
quantity_received = fields.Float(string="Quantity Received")
possible_reason = fields.Many2one('amgl.product_reason',string='Possible Reason')
possible_solution = fields.Many2one('amgl.product_solution', string='Possible Solution')
notes = fields.Html(string='Notes')
@api.model_cr
def init(self):
tools.drop_view_if_exists(self._cr, 'amgl_pending_accounts')
self._cr.execute("""
CREATE VIEW amgl_pending_accounts AS (
SELECT
min(o.id) as id,
c.name AS first_name,
c.last_name AS last_name,
sum(o.total_received_qty) AS quantity_received,
sum(o.total_qty) AS quantity_expected
FROM amgl_order AS o
INNER JOIN amgl_customer AS c ON c.id = o.customer_id
WHERE o.is_pending = True
Group By c.name,c.last_name
)""")
_auto=False : Determines whether a corresponding PostgreSQL table must be generated automatically from the object. Setting _auto to
False can be useful in case of OpenERP objects generated from
PostgreSQL views. See the "Reporting From PostgreSQL Views" section
for more details.
You must take min(o.id) as id in select query other wise it will give
you Error.
Your view name & table name must be equal amgl_pending_accounts .
Update your query and add missing fields by joining those tables, then
your error will be fixed.
这可能对您有所帮助。
在您的 select 查询中添加此行将解决问题:
Select row_number() OVER () as id , customer_id ..................
和
在你的模型中添加 customer_id = fields.Many2one(amgl_customer,'customer')
我在示例模块中工作,我正在创建一个视图,我想在其中显示基于条件和来自多个模型的记录。已经在创建中。
Pending Accounts
class PendingAccounts(models.Model):
_name = 'amgl.pending_accounts'
_description = 'Pending Account'
name = fields.Char()
first_name = fields.Char(string="First Name")
last_name = fields.Char(string="Last Name")
quantity_expected = fields.Float(string="Quantity Expected")
quantity_received = fields.Float(string="Quantity Received")
possible_reason = fields.Many2one('amgl.product_reason',string='Possible Reason')
possible_solution = fields.Many2one('amgl.product_solution', string='Possible Solution')
notes = fields.Html(string='Notes')
@api.model_cr
def init(self):
tools.drop_view_if_exists(self._cr, 'pending_accounts')
self._cr.execute("""
CREATE VIEW pending_accounts AS (
SELECT c.name AS first_name,
c.last_name AS last_name,
o.total_received_qty AS quantity_received,
o.total_qty AS quantity_expected
FROM amgl_order AS o
INNER JOIN amgl_customer AS c ON c.id = o.customer_id
WHERE o.is_pending = True
)""")
Pending Accounts View
<odoo>
<data>
<record id="amgl.pending_accounts_action_window" model="ir.actions.act_window">
<field name="name">Pending Accounts</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">amgl.pending_accounts</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
<!-- Add Text Here -->
</p><p>
<!-- More details about what a user can do with this object will be OK -->
</p>
</field>
</record>
<record id="amgl.pending_accounts_form" model="ir.ui.view">
<field name="name">Pending Accounts Form</field>
<field name="model">amgl.pending_accounts</field>
<field name="arch" type="xml">
<form string="Pending Inventories">
<sheet>
<group>
<field name="first_name"/>
<field name="last_name"/>
<field name="quantity_expected"/>
<field name="quantity_received"/>
<field name="possible_reason"/>
<field name="possible_solution"/>
<field name="notes"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="amgl.pending_accounts_tree" model="ir.ui.view">
<field name="name">Pending Account</field>
<field name="model">amgl.pending_accounts</field>
<field name="arch" type="xml">
<tree string="Pending Accounts">
<field name="first_name"/>
<field name="last_name"/>
<field name="quantity_expected"/>
<field name="quantity_received"/>
<field name="possible_reason"/>
<field name="possible_solution"/>
<field name="notes"/>
</tree>
</field>
</record>
</data>
</odoo>
所以我创建了一个模型并添加了查询以在模型的 init
方法中获取所有这些记录。但它没有在树 view.I 中显示任何记录,也没有在 pgadmin 中的查询 运行 中显示,并且有一条记录针对此查询,如附件所示。
我现在面临以下错误,
Traceback (most recent call last):
File "/home/ahsan/v10/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/ahsan/v10/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/home/ahsan/v10/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/ahsan/v10/odoo/service/model.py", line 101, in wrapper
return f(dbname, *args, **kwargs)
File "/home/ahsan/v10/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/home/ahsan/v10/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/home/ahsan/v10/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/home/ahsan/v10/addons/web/controllers/main.py", line 827, in
search_read
return self.do_search_read(model, fields, offset, limit, domain, sort)
File "/home/ahsan/v10/addons/web/controllers/main.py", line 849, in
do_search_read
offset=offset or 0, limit=limit or False, order=sort or False)
File "/home/ahsan/v10/odoo/models.py", line 4681, in search_read
records = self.search(domain or [], offset=offset, limit=limit, order=order)
File "/home/ahsan/v10/odoo/models.py", line 1518, in search
res = self._search(args, offset=offset, limit=limit, order=order, count=count)
File "/home/ahsan/v10/odoo/models.py", line 4242, in _search
self._cr.execute(query_str, where_clause_params)
File "/home/ahsan/v10/odoo/sql_db.py", line 141, in wrapper
return f(self, *args, **kwargs)
File "/home/ahsan/v10/odoo/sql_db.py", line 218, in execute
res = self._obj.execute(query, params)
ProgrammingError: column amgl_pending_accounts.id does not exist
LINE 1: SELECT "amgl_pending_accounts".id FROM "amgl_pending_account...
使用_auto=False执行init()方法。
是否应该创建数据库table(默认值:True)
如果设置为 False,覆盖 init() 以创建数据库 table。
编辑
在这种情况下,首先我们需要从数据库中永久删除 table。转到您的终端并按照以下命令操作:
sudo su postgres
psql <YourDatabaseName>
DROP TABLE amgl_pending_accounts CASCADE;
\q
现在刷新网络浏览器并升级模块。
如果你定义了 _name = 'amgl.pending.acounts'
试试这个odoo 预计数据库中 table 的名称是:amgl_pending_accounts 试试这个。只需了解 (.) 将是 table 名称中的 (_)。
class PendingAccounts(models.Model):
_name = 'amgl.pending.accounts'
_description = 'Pending Account'
_auto = False
name = fields.Char()
first_name = fields.Char(string="First Name")
last_name = fields.Char(string="Last Name")
quantity_expected = fields.Float(string="Quantity Expected")
quantity_received = fields.Float(string="Quantity Received")
possible_reason = fields.Many2one('amgl.product_reason',string='Possible Reason')
possible_solution = fields.Many2one('amgl.product_solution', string='Possible Solution')
notes = fields.Html(string='Notes')
@api.model_cr
def init(self):
tools.drop_view_if_exists(self._cr, 'pending_accounts')
self._cr.execute("""
CREATE VIEW amgl_pending_accounts AS (
SELECT
-- by default view expect the id columns to exists.
row_number() OVER () AS id,
c.name AS first_name,
c.last_name AS last_name,
o.total_received_qty AS quantity_received,
o.total_qty AS quantity_expected
FROM amgl_order AS o
INNER JOIN amgl_customer AS c ON c.id = o.customer_id
WHERE o.is_pending = True
)""")
XML:
<odoo>
<data>
<record id="amgl.pending.accounts_action_window" model="ir.actions.act_window">
<field name="name">Pending Accounts</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">amgl.pending.accounts</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
<!-- Add Text Here -->
</p><p>
<!-- More details about what a user can do with this object will be OK -->
</p>
</field>
</record>
<record id="amgl.pending.accounts_form" model="ir.ui.view">
<field name="name">Pending Accounts Form</field>
<field name="model">amgl.pending.accounts</field>
<field name="arch" type="xml">
<form string="Pending Inventories">
<sheet>
<group>
<field name="first_name"/>
<field name="last_name"/>
<field name="quantity_expected"/>
<field name="quantity_received"/>
<field name="possible_reason"/>
<field name="possible_solution"/>
<field name="notes"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="amgl.pending.accounts_tree" model="ir.ui.view">
<field name="name">Pending Account</field>
<field name="model">amgl.pending.accounts</field>
<field name="arch" type="xml">
<tree string="Pending Accounts">
<field name="first_name"/>
<field name="last_name"/>
<field name="quantity_expected"/>
<field name="quantity_received"/>
<field name="possible_reason"/>
<field name="possible_solution"/>
<field name="notes"/>
</tree>
</field>
</record>
</data>
</odoo>
您可以使用以下方法完成。
class PendingAccounts(models.Model):
_name = 'amgl.pending_accounts'
_description = 'Pending Account'
_auto=False
name = fields.Char()
first_name = fields.Char(string="First Name")
last_name = fields.Char(string="Last Name")
quantity_expected = fields.Float(string="Quantity Expected")
quantity_received = fields.Float(string="Quantity Received")
possible_reason = fields.Many2one('amgl.product_reason',string='Possible Reason')
possible_solution = fields.Many2one('amgl.product_solution', string='Possible Solution')
notes = fields.Html(string='Notes')
@api.model_cr
def init(self):
tools.drop_view_if_exists(self._cr, 'amgl_pending_accounts')
self._cr.execute("""
CREATE VIEW amgl_pending_accounts AS (
SELECT
min(o.id) as id,
c.name AS first_name,
c.last_name AS last_name,
sum(o.total_received_qty) AS quantity_received,
sum(o.total_qty) AS quantity_expected
FROM amgl_order AS o
INNER JOIN amgl_customer AS c ON c.id = o.customer_id
WHERE o.is_pending = True
Group By c.name,c.last_name
)""")
_auto=False : Determines whether a corresponding PostgreSQL table must be generated automatically from the object. Setting _auto to False can be useful in case of OpenERP objects generated from PostgreSQL views. See the "Reporting From PostgreSQL Views" section for more details.
You must take min(o.id) as id in select query other wise it will give you Error.
Your view name & table name must be equal amgl_pending_accounts .
Update your query and add missing fields by joining those tables, then your error will be fixed.
这可能对您有所帮助。
在您的 select 查询中添加此行将解决问题:
Select row_number() OVER () as id , customer_id .................. 和 在你的模型中添加 customer_id = fields.Many2one(amgl_customer,'customer')