2017-07-14 90 views
2

我正在一個示例模塊中工作,我正在創建一個視圖,其中我想根據條件和來自多個模型來顯示記錄。哪些已經被創建。Odoo - 初始化函數不起作用

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

回答

0

你可以用下面的方法去做。

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.

這可能會幫助你。

+0

我認爲它不工作也因爲現在我在下一個字段出錯'ProgrammingError:列amgl_pending_accounts.possible_solution不存在 線1:... amgl_pending_accounts「。 「last_name」作爲「last_name」,「amgl_pend ...」 – Ancient

+0

第一個問題是因爲ID字段,現在給我完整的錯誤,我會告訴你的原因 –

+0

請查看更新的答案,我的查詢中有錯誤。我忘了提及小組。 –

1

使用_auto =假執行的init()方法

無論是數據庫表應創建(默認:true)

如果設置爲False,重載的init()來創建數據庫表。

編輯

在這種情況下,首先我們需要從數據庫中永久刪除表。轉到您的終端並按照以下命令:

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

現在刷新網絡瀏覽器和升級模塊。

+0

我應該刪除表,如果它已經在db中創建? – Ancient

+0

謝謝,我試過,但它不工作 – Ancient

+0

現在我越來越'編程錯誤:關係「amgl_pending_accounts」不存在 LINE 1:SELECT「amgl_pending_accounts」.id FROM「amgl_pending_account ... '當我訪問樹視圖 – Ancient

0

試試這個,如果你定義_name = 'amgl.pending.acounts'

odoo excpect,在數據庫中的表的名稱是: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> 
+0

沒有它的同樣的錯誤。爲什麼只有當我加載樹視圖時纔會出錯,而不是在表單視圖中,而且如果您看到錯誤查詢,則會嘗試從'amgl_pending_accounts'中獲取記錄,並且該表不再存在。 – Ancient

+0

檢查我的編輯.. – Cherif

+0

'_auto = False'? – Ancient