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 new model record with sale order details in a smart button click?

Naroči se

Get notified when there's activity on this post

This question has been flagged
modelsbuttonsrecordsodoo13.0
7855 Prikazi
Avatar
avadoota

I am learning odoo myself and I got stuck with a problem, I have created a module named package and have added a package field to the sale order line, now I have to add a smart button when the sale order is confirmed, upon clicking it a new record must be created with fields

selected package in SO

sale order reference

expected delivery date

selected customer in SO

This smart button is no ordinary smart button that uses context to filter out existing data, but a button that creates a new record that can be seen in the inventory module's master data.

So my requirement is to create a new model named wrap with required fields which gets created when I click the said smart button.

How is this done? Thanks in advance.

my code-

Package model

from odoo import models, fields, api, _


class Package(models.Model):
  _name = 'sale.package'
  _description = 'Package table'

  name = fields.Char(string='Name', required=True)
  width = fields.Float(string='Width')
  height = fields.Float(string='Height')
  length = fields.Float(string='Length')
  maximum_weight = fields.Float(string='Maximum weight')
  package_sequence = fields.Char(string='Package reference', required=True, copy=False,
  readonly=True, index=True, default=lambda self: _('New Package'))

  @api.model
  def create(self, val):
  if val.get('package_sequence', _('New Package')) == _('New Package'):
  val['package_sequence'] = self.env['ir.sequence'].next_by_code('sale.package.sequence') or _('New Package')
  result = super(Package, self).create(val)
  return result


class SaleOrderPackage(models.Model):
  _inherit = 'sale.order'

  package_count = fields.Integer(compute='compute_package_count')
  packs = fields.Many2many('sale.package', string='Package')
  package_lines = fields.One2many('sale.package.lines', 'line_name_id', string='Package Lines')

  @api.onchange('packs')
  def _onchange_packs(self):
  for rec in self:
  lines = [(5, 0, 0)]
  for line in self.packs:
  values = {
  'name_on_line': line.name,
  'line_width': line.width,
  'line_height': line.height,
  'line_length': line.length,
  'line_maximum_weight': line.maximum_weight,
  }
  lines.append((0, 0, values))
  rec.package_lines = lines

  def get_packages(self):
  return {
  'name': 'Selected Packages',
  'domain': [('id', 'in', self.packs.ids)],
  'view_type': 'form',
  'res_model': 'sale.package',
  'view_id': False,
  'view_mode': 'tree,form',
  'type': 'ir.actions.act_window'
  }

  def compute_package_count(self):
  for record in self:
  record.package_count = self.env['sale.package'].search_count(
  [('id', 'in', self.packs.ids)])


class SaleOrderPackageLines(models.Model):
  _name = 'sale.package.lines'
  _description = 'One2many lines in sales order'

  line_name_id = fields.Many2one('sale.order', string='Package')
  name_on_line = fields.Char(string='Name')
  line_width = fields.Float(string='Width')
  line_height = fields.Float(string='Height')
  line_length = fields.Float(string='Length')
  line_maximum_weight = fields.Float(string='Maximum weight')


class SaleOrderLineTree(models.Model):
  _inherit = 'sale.order.line'

  packets = fields.Many2one('sale.package', string='Package')


package view

<?xml version="1.0" encoding="utf-8"?>

<odoo>

    <data>
<!--                        TREE VIEW                       -->
        <record id="packages_tree_view" model="ir.ui.view">
            <field name="name">package_tree</field>
            <field name="model">sale.package</field>
            <field name="arch" type="xml">
                <tree string="package">
                    <field name="package_sequence"/>
                    <field name="name"/>
                    <field name="maximum_weight" optional="hide"/>
                    <field name="length" optional="hide"/>
                    <field name="width" optional="hide"/>
                    <field name="height" optional="show"/>
                </tree>
            </field>
        </record>

<!--                    FORM VIEW                           -->
        <record id="packages_form_view" model="ir.ui.view">
            <field name="name">package_form</field>
            <field name="model">sale.package</field>
            <field name="arch" type="xml">
                <form string="package">
                    <sheet>
                        <div class="oe_title">
                            <h1>
                                <field name="package_sequence" readonly="1"/>
                            </h1>
                        </div>
                        <div style="height:50px">
                        </div>
                        <group>
                            <group>
                                <field name="name" placeholder="Name the package"/>
                                <div style="height:10px">
                                </div>
                                <field name="maximum_weight"/>
                            </group>
                            <group>
                                <field name="width"/>
                                <div style="height:5px">
                                </div>
                                <field name="height"/>
                                <div style="height:5px">
                                </div>
                                <field name="length"/>
                            </group>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

<!--                PACKAGE MENU BUTTON ACTION                  -->
        <record id="package_action" model="ir.actions.act_window">
            <field name="name">Packages</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">sale.package</field>
            <field name="view_mode">tree,form</field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    Create your packages here!
                </p>
            </field>
        </record>

<!--                SALE ORDER INHERITED VIEW           -->
        <record id="sale_order_inherit" model="ir.ui.view">
            <field name="name">sale_order_custom</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='partner_id']" position="after">
                    <field name="packs" widget="many2many_tags"/>
                </xpath>
                <xpath expr="//field[@name='order_line']/tree/field[@name='price_unit']" position="before">
                    <field name="packets"/>
                </xpath>
                <xpath expr="//page[3]" position="after">
                    <page string="Package Info">
                        <field name="package_lines">
                            <tree editable="bottom">
                                <field name="name_on_line"/>
                                <field name="line_width"/>
                                <field name="line_height"/>
                                <field name="line_length"/>
                                <field name="line_maximum_weight"/>
                            </tree>
                        </field>
                    </page>
                </xpath>
                <button name="action_view_invoice" position="after">
                    <button name="get_packages" type="object" class="oe_stat_button"
                            icon="fa-arrow-right" states="sale">
                        <field string="Packages" name="package_count" widget="statinfo"/>
                    </button>
                </button>
            </field>
        </record>

<!--                            PACKAGE MENU BUTTON                                         -->
        <menuitem id="create_package_menu" name="Packages" action="package_action"
                  parent="stock.menu_stock_config_settings" sequence="2"/>

    </data>

</odoo>

 wrap model

from odoo import models, fields, api, _


class Wrap(models.Model):
    _name = 'sale.wrap'
    _description = 'wraps in SO tree view'

    name = fields.Char(string='Name', required=True)
    product = fields.Many2one('product.product', string='Product')
    quantity = fields.Integer()
    wrap_sequence = fields.Char(string='Wrap reference', required=True, copy=False,
                                readonly=True, index=True, default=lambda self: _('New Wrap'))

    @api.model
    def create(self, val):
        if val.get('wrap_sequence', _('New Wrap')) == _('New Wrap'):
            val['wrap_sequence'] = self.env['ir.sequence'].next_by_code('sale.wrap.sequence') or _('New Wrap')
        result = super(Wrap, self).create(val)
        return result


class SaleOrderWrap(models.Model):
    _inherit = 'sale.order'

    def get_wraps(self):
        return {}


wrap view

<?xml version="1.0" encoding="utf-8"?>

<odoo>

    <data>

<!--                        Wrap tree view                       -->
        <record id="wrap_tree_view" model="ir.ui.view">
            <field name="name">Wrap_tree</field>
            <field name="model">sale.wrap</field>
            <field name="arch" type="xml">
                <tree string="wrap">
                    <field name="wrap_sequence"/>
                    <field name="name"/>
                    <field name="product"/>
                    <field name="quantity"/>
                </tree>
            </field>
        </record>

<!--                        Wrap form view                      -->
        <record id="wrap_form_view" model="ir.ui.view">
            <field name="name">Wrap_form</field>
            <field name="model">sale.wrap</field>
            <field name="arch" type="xml">
                <form string="wrap">
                    <sheet>
                        <div class="oe_title">
                            <h1>
                                <field name="wrap_sequence" readonly="1"/>
                            </h1>
                        </div>
                        <div style="height:50px"/>
                        <group>
                            <group>
                                <field name="name" placeholder="Name the wrap"/>
                            </group>
                            <group>
                                <field name="product"/>
                                <div style="height:10px"/>
                                <field name="quantity"/>
                            </group>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

<!--                        Sale order line inherited view      -->
        <record id="sale_order_wrap_inherit" model="ir.ui.view">
            <field name="name">sale_order_custom_wrap</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml">
                <xpath expr="//button[@name='action_view_invoice']" position="before">
                    <button name="get_wraps" type="object" class="oe_stat_button"
                            icon="fa-arrow-left" states="sale" string="Wraps"/>
                </xpath>
            </field>
        </record>

<!--                        Action for menu item wrap                -->
        <record id="wrap_action" model="ir.actions.act_window">
            <field name="name">Wrap</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">sale.wrap</field>
            <field name="view_mode">tree,form</field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    Create your Wraps here!
                </p>
            </field>
        </record>

<!--                        Wrap menu item                           -->
        <menuitem id="create_wrap_menu" name="Wraps" action="wrap_action"
                  parent="stock.menu_stock_inventory_control" sequence="5"/>

    </data>

</odoo>


0
Avatar
Opusti
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 two different models in a list view
models records design
Avatar
1
jul. 19
8541
Button on form to save record
views buttons records
Avatar
Avatar
1
jun. 17
8456
Emergency please, New user preference don't show !!! Solved
models records preferences
Avatar
Avatar
1
maj 16
5025
Model Button (next To Create)
models buttons v8.0
Avatar
0
jun. 15
4056
Python list in records.
models records model lists
Avatar
1
nov. 20
4570
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