Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

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

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

How to create new model record with sale order details in a smart button click?

Subscribe

Get notified when there's activity on this post

This question has been flagged
modelsbuttonsrecordsodoo13.0
8031 Views
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
Discard
Enjoying the discussion? Don't just read, join in!

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

Sign up
Related Posts Replies Views Activity
Merge two different models in a list view
models records design
Avatar
1
Jul 19
8612
Button on form to save record
views buttons records
Avatar
Avatar
1
Jun 17
8508
Emergency please, New user preference don't show !!! Solved
models records preferences
Avatar
Avatar
1
May 16
5092
Model Button (next To Create)
models buttons v8.0
Avatar
0
Jun 15
4133
Python list in records.
models records model lists
Avatar
1
Nov 20
4659
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة 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