Search code examples
odooodoo-8odoo-9odoo-10odoo-view

Odoo - init function is not working


I am working in a sample module and i am creating a view in which i wanted to display record based on criteria and from multiple models. Which are already being created.

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>

So i created a model and added query to get all those records in init method of model. But its not showing any records in tree view.I also run the query in pgadmin and there is a records coming against this query as shown in attachment. enter image description here

I am facing following error now,

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

Solution

  • You can do it using the following method.

    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.

    This may help you.