pythonodooone-to-manyodoo-viewodoo-12

How to add One2many relation in header-details invoice mod in odoo 12 properly?


i trying to build header-details relation for invoice module, but i don't really get how odoo 12 work for one2many relation, i have module named ms_produk, and my database name is "ms_produk_ms_produk", ms_produk module work as product master, which is CRUD product list, and i want the module to choose product when creating an invoice

here is what i do, i create a model that looked like this :

# -*- 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())
    nm_sales = fields.Selection(
        [('bruno', 'Bruno'),('layla','Layla'),('vexana','Vexana')]
        ,string='Choose Seller')
    keterangan = fields.Html(string='Keterangan')
    kd_sales = fields.Many2one('res.users', string='Kode Sales')
    details = fields.One2many('ms_produk_ms_produk','no_faktur','No Faktur')

but when the details written like this, it give me error like "Internal Server Error" the server won't start, then i changed it to :

# -*- 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())
            nm_sales = fields.Selection(
        [('bruno', 'Bruno'),('layla','Layla'),('vexana','Vexana')]
        ,string='Choose Seller')
        ,string='Pilih Sales')
    keterangan = fields.Html(string='Keterangan')
    kd_sales = fields.Many2one('res.users', string='Kode Sales')
    details = fields.One2many('salesorder.salesorder','no_faktur','No Faktur')

this works but wrong, it load the salesorder it self, what i want is, it appear product list, and i pick the product name and add it to the invoice details.

and here is my views, i named it salesorder_view.xml

<?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>

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

how to do that properly?


Solution

  • While define One2many field you need to give the relation to table which you need to load, Please refer this link to know about One2many fields.

    By using form view you can design view of your model.