Skip to Content
Menu
This question has been flagged

Hi, 

I would like to copy and create my own new sale.order model and save data into a separate table in database. Therefore, I am using '_inherit' with '_name' in new model.

However, sale.order also include sale.order.line then I create my own new 'sale.order.line' as above.

When I tried to click Save button, I got the error below: 

-> raise TypeError("Mixing apples and oranges: %s - %s" % (self, other))

Here is my source code

class MyOrder(models.Model):
_name = "my.order"
_inherit = 'sale.order'
_description = "Sale Order"

order_line = fields.One2many('my.order.line', 'order_id', string='Order Lines',
states={'cancel': [('readonly', True)], 'done': [('readonly', True)]}, copy=True,
auto_join=True)
state = fields.Selection([
('draft', 'Draft'),
('sale', 'Sales Order'),
('done', 'Locked'),
('cancel', 'Cancelled'),
], string='Status', readonly=True, copy=False, index=True, track_visibility='onchange', track_sequence=3,
default='draft')

@api.model_cr
def init(self):
self._cr.execute("""
ALTER TABLE public.my_order ALTER COLUMN warehouse_id DROP NOT NULL;
ALTER TABLE public.my_order ALTER COLUMN picking_policy DROP NOT NULL;
ALTER TABLE public.my_order ALTER COLUMN partner_invoice_id DROP NOT NULL;
ALTER TABLE public.my_order ALTER COLUMN partner_shipping_id DROP NOT NULL;
ALTER TABLE public.my_order ALTER COLUMN team_group DROP NOT NULL;
""")

@api.model
def create(self, vals):
if vals.get('name', _('New')) == _('New'):
if 'company_id' in vals:
vals['name'] = self.env['ir.sequence'].with_context(force_company=vals['company_id']).next_by_code(
'my.order') or _('New')
else:
vals['name'] = self.env['ir.sequence'].next_by_code('my.order') or _('New')

# Makes sure 'pricelist_id' are defined
if any(f not in vals for f in ['pricelist_id']):
partner = self.env['res.partner'].browse(vals.get('partner_id'))
vals['pricelist_id'] = vals.setdefault('pricelist_id',
partner.property_product_pricelist and partner.property_product_pricelist.id)
# Change state of order to 'sale'
vals['state'] = 'sale'
vals['date_order'] = fields.Date.today()
return super(MyOrder, self).create(vals)

class MyOrderLine(models.Model):
_name = 'my.order.line'
_inherit = 'sale.order.line'
_description = 'Sales Order Line'

order_id = fields.Many2one('my.order', string='Order Reference', required=True, ondelete='cascade',
index=True,
copy=False)

state = fields.Selection([
('draft', 'Draft'),
('sale', 'Sales Order'),
('done', 'Done'),
('cancel', 'Cancelled'),
], related='order_id.state', string='Order Status', readonly=True, copy=False, store=True, default='draft')

@api.model_cr
def init(self):
self._cr.execute("""
ALTER TABLE public.my_order_line ALTER COLUMN customer_lead DROP NOT NULL;
""")

# Override for non-checking inventory
@api.onchange('product_uom_qty')
def _onchange_product_uom_qty(self):
return {}

# Override for non-checking inventory
@api.onchange('product_uom_qty', 'product_uom', 'route_id')
def _onchange_product_id_check_availability(self):
return {}


Thank you for your help!

Avatar
Discard

Better you can use inherits the model sale.order such a way that while creating record it will get added in both models. By default in odoo, you will get example with the product.product model and product.template model.

In the product.product model there is a many2one relation to product.template model, and also product.product model is created using the _inherits

Author

But it inherits only fields not methods, right?

I still want to use some compute methods in sale.order to compute price or total.

Related Posts Replies Views Activity
2
Jan 24
3668
0
Jun 23
939
1
Dec 22
1955
0
Apr 22
1399
1
Jun 19
6922