Odoo-init功能不起作用

时间:2017-07-14 16:06:02

标签: openerp odoo-8 odoo-9 odoo-10 odoo-view

我正在一个示例模块中工作,我正在创建一个视图,我希望根据条件和多个模型显示记录。哪些已经创建。

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模型方法中的所有记录。但它没有在树视图中显示任何记录。我还在pgadmin中运行查询,并且有一个针对此查询的记录,如附件中所示。 enter image description here

我现在面临以下错误,

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...

4 个答案:

答案 0 :(得分:1)

使用 _auto = False 执行 init()方法。

是否应创建数据库表(默认值:True)

如果设置为False,则覆盖init()以创建数据库表。

修改

在这种情况下,首先我们需要从数据库中永久删除表。转到您的终端并按照以下命令:

sudo su postgres
psql <YourDatabaseName>
DROP TABLE amgl_pending_accounts CASCADE;
\q

现在刷新Web浏览器和升级模块。

答案 1 :(得分:1)

您可以使用以下方法执行此操作。

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 :确定是否必须从对象自动生成相应的PostgreSQL表。将_auto设置为   如果生成OpenERP对象,则False非常有用   PostgreSQL视图。请参阅&#34;从PostgreSQL视图报告&#34;部分   了解更多详情。

     

你必须在选择查询中将 min(o.id)作为id ,否则它会给出   你错了。

     

您的查看名称&amp;表名必须等于 amgl_pending_accounts

     

通过加入这些表来更新您的查询并添加缺少的字段   你的错误将被修复。

这可能会对你有帮助。

答案 2 :(得分:0)

如果您定义_name =&#39; amgl.pending.acounts&#39;请尝试此操作。

odoo怀疑数据库中表的名称是:amgl_pending_accounts 试试这个。只要明白(。)将是表名中的(_)。

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>

答案 3 :(得分:0)

在您的选择查询中添加此行,它将解决问题:

选择row_number()OVER()作为id,customer_id ................ 和 在模型中添加customer_id = fields.Many2one(amgl_customer,'customer')