Skip to Content
Menu
This question has been flagged

I want my module to be able to add products like when you want to add products in sale quotation. Can you tell me how or reference me to a good link? Thanks

Avatar
Discard
Best Answer

Hi,
To get a many2one field of a product , you can add a field like this,

product_field = fields.Many2one('product.product', string="Product")

To get the same as in the sales form,

In the Py


class TestA(models.Model):
_name = 'test.a

add_product_lines = fields.One2many('test.b', 'connect_test_b', string='Products')

class TestB(models.Model):
_name = 'test.b'

connect_test_b = fields.Many2one('test.a', string="Connect")
product_id = fields.Many2one('product.product', string="Product")
description = fields.Text(string="Description")
pro_qty = fields.Integer(string='Quantity', default=1)
unit_price = fields.Float(string='Unit Price')
tax = fields.Many2one('account.tax', string='Tax')
sub_total = fields.Float(string='Sub Total')

Then in the Xml,

<page string="Product Service">
<field name="add_product_lines">
<tree editable="bottom">
<field name="product_id"/>
<field name="description"/>
<field name="pro_qty"/>
<field name="unit_price"/>
<field name="tax"/>
<field name="sub_total"/>
</tree>
</field>

</page>

Thanks


Avatar
Discard
Author Best Answer

Error context:
View `sales_orders.form`
[view_id: 792, xml_id: contract.sales_orders_form_view, model: contract.sales_orders, parent_id: n/a]
2017-07-03 09:08:07,631 28860 INFO test werkzeug: 127.0.0.1 - - [03/Jul/2017 09:08:07] "GET / HTTP/1.1" 500 - 2017-07-03 09:08:07,650 28860 ERROR test werkzeug: Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 177, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 165, in execute
    application_iter = app(environ, start_response)
  File "/home/christian/Odoo-8.0/openerp/service/server.py", line 295, in app
    return self.app(e, s)
  File "/home/christian/Odoo-8.0/openerp/service/wsgi_server.py", line 216, in application
    return application_unproxied(environ, start_response)
  File "/home/christian/Odoo-8.0/openerp/service/wsgi_server.py", line 202, in application_unproxied
    result = handler(environ, start_response)
  File "/home/christian/Odoo-8.0/openerp/http.py", line 1297, in __call__
    return self.dispatch(environ, start_response)
  File "/home/christian/Odoo-8.0/openerp/http.py", line 1271, in __call__
    return self.app(environ, start_wrapped)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 588, in __call__
    return self.app(environ, start_response)
  File "/home/christian/Odoo-8.0/openerp/http.py", line 1442, in dispatch
    ir_http = request.registry['ir.http']
  File "/home/christian/Odoo-8.0/openerp/http.py", line 353, in registry
    return openerp.modules.registry.RegistryManager.get(self.db) if self.db else None
  File "/home/christian/Odoo-8.0/openerp/modules/registry.py", line 339, in get
    update_module)
  File "/home/christian/Odoo-8.0/openerp/modules/registry.py", line 370, in new
    openerp.modules.load_modules(registry._db, force_demo, status, update_module)
  File "/home/christian/Odoo-8.0/openerp/modules/loading.py", line 351, in load_modules
    force, status, report, loaded_modules, update_module)
  File "/home/christian/Odoo-8.0/openerp/modules/loading.py", line 255, in load_marked_modules
    loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
  File "/home/christian/Odoo-8.0/openerp/modules/loading.py", line 176, in load_module_graph
    _load_data(cr, module_name, idref, mode, kind='data')
  File "/home/christian/Odoo-8.0/openerp/modules/loading.py", line 118, in _load_data
    tools.convert_file(cr, module_name, filename, idref, mode, noupdate, kind, report)
  File "/home/christian/Odoo-8.0/openerp/tools/convert.py", line 897, in convert_file
    convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
  File "/home/christian/Odoo-8.0/openerp/tools/convert.py", line 983, in convert_xml_import
    obj.parse(doc.getroot(), mode=mode)
  File "/home/christian/Odoo-8.0/openerp/tools/convert.py", line 849, in parse
    self._tags[rec.tag](self.cr, rec, n, mode=mode)
  File "/home/christian/Odoo-8.0/openerp/tools/convert.py", line 759, in _tag_record
    id = self.pool['ir.model.data']._update(cr, self.uid, rec_model, self.module, res, rec_id or False, not self.isnoupdate(data_node), noupdate=self.isnoupdate(data_node), mode=self.mode, context=rec_context )
  File "/home/christian/Odoo-8.0/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/christian/Odoo-8.0/openerp/addons/base/ir/ir_model.py", line 1023, in _update
    model_obj.write(cr, uid, [res_id], values, context=context)
  File "/home/christian/Odoo-8.0/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/christian/Odoo-8.0/openerp/addons/base/ir/ir_ui_view.py", line 281, in write
    context)
  File "/home/christian/Odoo-8.0/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/christian/Odoo-8.0/openerp/api.py", line 399, in old_api
    result = method(recs, *args, **kwargs)
  File "/home/christian/Odoo-8.0/openerp/models.py", line 3792, in write
    self._write(old_vals)
  File "/home/christian/Odoo-8.0/openerp/api.py", line 266, in wrapper
    return new_api(self, *args, **kwargs)
  File "/home/christian/Odoo-8.0/openerp/api.py", line 592, in new_api
    result = method(self._model, cr, uid, self.ids, *args, **old_kwargs)
  File "/home/christian/Odoo-8.0/openerp/models.py", line 3971, in _write
    recs._validate_fields(vals)
  File "/home/christian/Odoo-8.0/openerp/api.py", line 266, in wrapper
    return new_api(self, *args, **kwargs)
  File "/home/christian/Odoo-8.0/openerp/models.py", line 1275, in _validate_fields
    raise ValidationError('\n'.join(errors))
ParseError: "ValidateError Field(s) `arch` failed against a constraint: Invalid view definition

Error details:
Field `description` does not exist

Error context:
View `sales_orders.form`
[view_id: 792, xml_id: contract.sales_orders_form_view, model: contract.sales_orders, parent_id: n/a]" while parsing /home/christian/Odoo-8.0/custom-addons/contract/views/sales_orders.xml:4, near
<record model="ir.ui.view" id="sales_orders_form_view">
            <field name="name">sales_orders.form</field>
            <field name="model">contract.sales_orders</field>
            <field name="arch" type="xml">
                <form string="Contract Sales Orders Form">
                    <header>
                        <button name="action_draft" type="object" string="Reset to draft" states="running"/>
                        <button name="action_running" type="object" string="Run" states="draft" class="oe_highlight"/>
                        <button name="action_done" type="object" string="Mark as done" states="running" class="oe_highlight"/>
                        <button name="action_canceled" type="object" string="Cancel" states="draft,running"/>
                        <field name="state" widget="statusbar"/>
                    </header>

                    <sheet>
                        <group>
                            <field name="order_number"/>
                            <field name="partner_id"/>
                            <field name="date_contract"/>
                            <field name="interval"/>
                            <field name="interval_type"/>
                            <field name="number_of_call"/>
                            <!-- <field name="line_ids" attrs="{
                            'readonly':[('state','!=','draft')]
                            }">
                                <tree editable="bottom">
                                    <field name="product_id"/>
                                    <field name="price"/>
                                    <field name="qty"/>
                                    <field name="uom"/>
                                    <field name="subtotal" sum="Total"/>


                                </tree>
                            </field> -->
                            <field name="next_date_execute"/>
                            <field name="remain_interval"/>
                        </group>
                        <notebook>
                            <page string="Order Lines">
                                <field name="add_product_lines">
                                    <tree editable="bottom">
                                        <field name="product_id"/>
                                        <field name="description"/>
                                        <field name="pro_qty"/>
                                        <field name="unit_price"/>
                                        <field name="tax"/>
                                        <field name="sub_total"/>
                                    </tree>
                                </field>
                            </page>
                            <page string="About">
                                This is an example of notebooks
                            </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>

I got that error.

I try commenting the description field, then I got the product_id not exist error.

Avatar
Discard
Author

ah, I get where I did wrong.

I delete the TestA class and insert add_product_lines = fields.One2many('test.b', 'connect_test_b', string='Products') to my class. Now it works. Thanks.

Can you show me the py file

Author

# -*- coding: utf-8 -*-

from openerp import models, fields, api

class sales_orders(models.Model):

_name = 'contract.sales_orders'

add_product_lines = fields.One2many('test.b', 'connect_test_b', string='Products')

order_number = fields.Char(string="Order Number")

partner_id = fields.Many2one('res.partner', string="Customer")

# line_ids = fields.One2many('sale.order.contract.line', 'contract_id',

# string="Products")

date_contract = fields.Date(string="Date Contract",

default=fields.Date.today)

interval = fields.Integer(string='Interval')

interval_type = fields.Selection(

[('day', 'Day'), ('week', 'Week'), ('month', 'Month')],

string="Interval Type", default='day')

number_of_call = fields.Integer(string="Number of Call", default=1)

next_date_execute = fields.Date(string="Next Date Execute")

remain_interval = fields.Integer(string="Remain Interval")

order_lines = fields.Text()

# Many2one('product.product', string="Product")

state = fields.Selection([

('draft', 'Draft'),

('running', 'Running'),

('done', 'Done'),

('canceled', 'Canceled')

], string="State", default='draft')

@api.multi

def action_draft(self):

self.state = 'draft'

@api.multi

def action_running(self):

self.state = 'running'

@api.multi

def action_done(self):

self.state = 'done'

@api.multi

def action_canceled(self):

self.state = 'canceled'

# class TestA(models.Model):

# _name = 'test.a'

# add_product_lines = fields.One2many('test.b', 'connect_test_b', string='Products')

class TestB(models.Model):

_name = 'test.b'

# @api.multi

# def _get_subtotal(self):

# for data in self:

# data.subtotal=data.unit_price * data.pro_qty

@api.multi

def _get_subtotal(self):

for data in self:

data.subtotal=data.price * data.qty

connect_test_b = fields.Many2one('contract.sales_orders', string="Connect")

# product_id = fields.Many2one('product.product', string="Product")

description = fields.Text(string="Description")

# pro_qty = fields.Integer(string='Quantity', default=1)

# uom=fields.Many2one('product.uom',string='Unit of Measure')

# unit_price = fields.Float(string='Unit Price')

tax = fields.Many2one('account.tax', string='Tax')

# subtotal = fields.Float(string='Sub Total', compute=_get_subtotal)

product_id=fields.Many2one('product.product',string='Product')

qty=fields.Integer(string="Quantity")

price=fields.Float(string="Unit Price")

subtotal=fields.Float(string="Sub Total",compute=_get_subtotal)

uom=fields.Many2one('product.uom',string='Unit of Measure')

@api.onchange('product_id')

# onchange price dan uom

def onchange_price(self):

self.price=self.product_id.list_price

self.uom=self.product_id.uom_id.id

@api.onchange('qty')

# onchange subtotal

def onchange_subtotal(self):

self.subtotal=self.price * self.qty

# @api.onchange('product_id')

# # onchange price dan uom

# def onchange_unit_price(self):

# self.unit_price=self.product_id.list_price

# self.uom=self.product_id.uom_id.id

# @api.onchange('pro_qty')

# # onchange subtotal

# def onchange_subtotal(self):

# self.subtotal=self.unit_price * self.pro_qty

Related Posts Replies Views Activity
0
Mar 15
3754
0
Jun 15
2903
1
Apr 21
31444
2
Aug 24
4678
1
Dec 23
1110