Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Aplikacije
    Finance
    • Knjigovodstvo
    • Obračun
    • Stroški
    • Spreadsheet (BI)
    • Dokumenti
    • Podpisovanje
    Prodaja
    • CRM
    • Prodaja
    • POS Shop
    • POS Restaurant
    • Naročnine
    • Najem
    Spletne strani
    • Website Builder
    • Spletna trgovina
    • Blog
    • Forum
    • Pogovor v živo
    • eUčenje
    Dobavna veriga
    • Zaloga
    • Proizvodnja
    • PLM
    • Nabava
    • Vzdrževanje
    • Kakovost
    Kadri
    • Kadri
    • Kadrovanje
    • Odsotnost
    • Ocenjevanja
    • Priporočila
    • Vozni park
    Marketing
    • Družbeno Trženje
    • Email Marketing
    • SMS Marketing
    • Dogodki
    • Avtomatizacija trženja
    • Ankete
    Storitve
    • Projekt
    • Časovnice
    • Storitve na terenu
    • Služba za pomoč
    • Načrtovanje
    • Termini
    Produktivnost
    • Razprave
    • Odobritve
    • IoT
    • Voip
    • Znanje
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industrije
    Trgovina na drobno
    • Book Store
    • Trgovina z oblačili
    • Trgovina s pohištvom
    • Grocery Store
    • Trgovina s strojno opremo računalnikov
    • Trgovina z igračami
    Food & Hospitality
    • Bar and Pub
    • Restavracija
    • Hitra hrana
    • Guest House
    • Beverage Distributor
    • Hotel
    Nepremičnine
    • Real Estate Agency
    • Arhitekturno podjetje
    • Gradbeništvo
    • Estate Management
    • Vrtnarjenje
    • Združenje lastnikov nepremičnin
    Svetovanje
    • Računovodsko podjetje
    • Odoo Partner
    • Marketinška agencija
    • Law firm
    • Pridobivanje talentov
    • Audit & Certification
    Proizvodnja
    • Tekstil
    • Metal
    • Pohištvo
    • Hrana
    • Brewery
    • Poslovna darila
    Health & Fitness
    • Športni klub
    • Trgovina z očali
    • Fitnes center
    • Wellness Practitioners
    • Lekarna
    • Frizerski salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Sistemi sončne energije
    • Izdelovalec čevljev
    • Čistilne storitve
    • HVAC Services
    Ostali
    • Neprofitna organizacija
    • Agencija za okolje
    • Najem oglasnih panojev
    • Fotografija
    • Najem koles
    • Prodajalec programske opreme
    Browse all Industries
  • Skupnost
    Learn
    • Tutorials
    • Dokumentacija
    • Certifikati
    • Šolanje
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Prenesi
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Dogodki
    • Prevodi
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Sklici kupca
    • Podpora
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Določanje cen
  • Pomoč

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

How to create and save my own Sale.Order model?

Naroči se

Get notified when there's activity on this post

This question has been flagged
sale.order.linesale.order12.
8919 Prikazi
Avatar
thanhps

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!

0
Avatar
Opusti
Niyas Raphy (Walnut Software Solutions)

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

thanhps
Avtor

But it inherits only fields not methods, right?

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

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Prijavi
Related Posts Odgovori Prikazi Aktivnost
Merge Same Item in Sale Order Line Solved
sale.order.line sale.order
Avatar
Avatar
Avatar
2
jan. 24
6260
Combine inline editing with more detailed form view
sale.order.line sale.order
Avatar
0
jun. 23
2748
ValueError: <class 'ValueError'>: "Expected singleton: sale.order.line(<NewId ref='virtual_117'>, <NewId ref='virtual_131'>)"
sale.order.line sale.order
Avatar
Avatar
1
dec. 22
4185
How to update a custom field as "Margin" default from sale.order does, when some product of sale.order.line is updated/deleted.
sale.order.line sale.order
Avatar
0
apr. 22
3218
How to get partner_id from sale.order.line? Solved
sale.order.line 12.
Avatar
Avatar
1
jun. 19
9505
Community
  • Tutorials
  • Dokumentacija
  • Forum
Open Source
  • Prenesi
  • Github
  • Runbot
  • Prevodi
Services
  • Odoo.sh Hosting
  • Podpora
  • Nadgradnja
  • Custom Developments
  • Izobraževanje
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Sredstva blagovne znamke
  • Kontakt
  • Zaposlitve
  • Dogodki
  • Podcast
  • Blog
  • Stranke
  • Pravno • Zasebnost
  • Varnost
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now