3

i trying to create fields.One2Many for my invoice module, i create two different module, one called ms_produk and another called invoice, then for the ms_produk i use as master product, which serve CRUD for item and have a table named "ms_produk_ms_produk"

here is my ms_produk model looked like :

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

from odoo import models, fields, api

class ProdukProduk(models.Model):
    _name = 'ms_produk.ms_produk'

    kd_produk = fields.Char(String='Kode Produk', required=True)
    nm_produk = fields.Char(String='Nama Produk', required=True)
    tanggal_input = fields.Datetime(string='Tanggal Input', default=fields.Datetime.now())
    tanggal_aktif = fields.Datetime(string='Tanggal Aktif', default=fields.Datetime.now())
    status_aktif = fields.Boolean('Status Aktif', default=True)
    keterangan = fields.Html(string='Keterangan')

    no_faktur = fields.Many2one('salesorder.salesorder')

i add

no_faktur = fields.Many2one('salesorder.salesorder')

to link this table to my invoice module, since i want to use One2Many function at my invoice module

then here is my invoice module model looked like, this model named "salesorder.py" :

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

from odoo import models, fields, api

class SalesorderSalesorder(models.Model):
    _name = 'salesorder.salesorder'

    no_faktur = fields.Char(String='No Faktur', required=True)
    kd_dealer = fields.Char(String='Kode Dealer', required=True)
    nm_dealer = fields.Char(String='Nama Dealer', required=True)
    tanggal_faktur = fields.Datetime(string='Tgl Faktur', default=fields.Datetime.now())
    keterangan = fields.Html(string='Keterangan')
    kd_sales = fields.Many2one('res.users', string='Kode Sales')
    details = fields.One2many('ms_produk.ms_produk','no_faktur','List Item')

this worked, when i click "add a line" it pop out my master product module, but it show in input form, how i changed it to list only? so i can pick some item to add as detail for my invoice?

SNAPSHOT : salesorder form : enter image description here

when i click "add a line", it showed up a form like this : (this is input form, i need list view of my product)

enter image description here

it should be a list of my item produk, that looked like this : (this is snapshot from my master module)

enter image description here

then i will added like this : (this is a example snapshot i take from Thayif Kabir link

enter image description here

**UPDATED CODE as @Ajmal JK answer, i tried to edit my salesorder_view.xml, and breakdown some code, here is how it looked like now :

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>

        <record id="salesorder_menu_action" model="ir.actions.act_window">
            <field name="name">SalesOrders</field>
            <field name="res_model">salesorder.salesorder</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="domain">[]</field>
            <field name="help" type="html">
                <p class="oe_view_nocontent_create">Create SalesOrders
                </p>
            </field>
        </record>

<!-- newly added -->

        <!-- salesorder view -->
        <record id="salesorder_tree" model="ir.ui.view">
            <field name="name">salesorder.form.tree</field>
            <field name="model">salesorder.salesorder</field>
            <field name="type">tree</field>
            <field name='arch' type="xml">
                <field name='details'>
                    <tree string="SalesOrder" editable="bottom">
                        <field name="kd_produk"/>
                        <field name="nm_produk"/>
                    </tree>
                </field>
            </field>
        </record>
        <!-- end of salesorder view -->

        <!-- master product view -->
        <record id="browse_msproduk_tree_view" model="ir.ui.view">
            <field name="name">ms_produk</field>
            <field name="model">ms_produk.ms_produk</field>
            <field name="view_mode">tree</field>
            <field name="arch" type="xml">
                <tree editable="bottom">
                    <field name="kd_produk"/>
                    <field name="nm_produk"/>
                    <field name="tanggal_input"/>
                </tree>
            </field>
            <field name="view_id" ref="salesorder_tree"/>
            <field name="act_window_id" ref="salesorder_menu_action"/>
        </record>
        <!-- end of master product view -->

<!-- end of newly added -->

        <menuitem id="salesorder_menu" name="SalesOrder"/>
        <menuitem id="Salesorder_neworder_menu" 
                parent="salesorder_menu" 
                name="New Order"
                action="salesorder_menu_action"/>
    </data>
</odoo>

there is no error when i run this code, it worked but not how i want it to be, when giving this code <tree editable="bottom"> in Master Product View section they become inline editable, so i have to type each column, what i need is a popup window that contain list of product so i can pick it and put in the details form, i try do <tree editable="bottom"> then i try <tree string="details"> with no editable tag, it comeback to popup but it's an input form, it should be list of product so i can check the item and add it into the details table, how do i do this?

Ke Vin
  • 3,478
  • 11
  • 60
  • 91

3 Answers3

3

Change the newly added section as,

       <record id="salesorder_tree" model="ir.ui.view">
        <field name="name">salesorder.form.tree</field>
        <field name="model">salesorder.salesorder</field>
        <field name="type">tree</field>
        <field name='arch' type="xml">
            <field name='details'>
               <tree string="SalesOrder" editable="bottom">
                  <field name="kd_produk"/>
                  <field name="nm_produk"/>
               </tree>
           </field>
        </field>
    </record>
Ajmal JK
  • 813
  • 4
  • 14
  • hi @Ajmal JK your code seems worked, but i need solve my master product view too, i already update my code in my question, seems editable="bottom" is used for inline editing, i need popup form when add item in details table, how to do that? i try to make it a popup, but it keep coming to input form, i need it to be list product and popup – Ke Vin Jul 15 '19 at 08:44
  • hi it worked now, i still have another issue, i will start another question, i think i code a wrong logic since i still studying this new framework, thank you – Ke Vin Jul 16 '19 at 03:15
  • @KeVin You said " i still have another issue" , but you didn't explain it , what is your new issue ? – Ajmal JK Jul 16 '19 at 04:44
  • i think i have wrong programming logic for odoo in this question, kind of mixed. in my question i ask about 2 model which is header invoice linked to one2many to my product master, which is i just think about it, it's doesn't make sense, i need relation like header invoice -> detail invoice --> detail invoice that link to product master to fill the detail table, is it okay i edit my question here? – Ke Vin Jul 17 '19 at 01:17
2

In your view define the fields you want in inside One2many field and make sure you've added editable in tree view. If you want to add records from another model to field in your model you can use Many2many field and can choose product from list view. Many2many field can be used as tree view or other view using widget Check this link to know more about relational fields

Thayif kabir
  • 715
  • 5
  • 20
2

@Terrence Poe my bad, i already update my code to form view, but still error, can u help me with full code?

define your x2m field as a list.

<!-- form view -->
<record model="ir.ui.view" id="browse_msproduk_form_view">
    <field name="name">salesorder.form</field>
    <field name="model">salesorder.salesorder</field>
    <field name="arch" type="xml">
        <form string="SalesOrder form">
            <field name='details'>
                <tree string="SalesOrder">
                    <field name="kd_produk"/>
                    <field name="nm_produk"/>
                </tree>
            </field>
    </form>
</record>
Terrence Poe
  • 634
  • 4
  • 17