pythonodooodoo-12

How to add domain filter in many2one odoo notebook tree view


I have a field one2many where in that field has a relationship between stock.trip one2many and stock.trip.sn.scanned, I want to display data from stock.trip.sn.scanned based on its status in the notebook treeview, so the image is like this enter image description here

So like the picture above I have 2 tabs but still with one field one2many, which can distinguish the status between "Registered" and "Unregistered". i've done it in xml by giving domain to that field but it doesn't work

 <notebook>
                    <page string="Scan barcode">
                        <field name="trip_scanned" domain="[('new_register','=','old')]">
                            <tree create="false" edit="false">
                                <field name="number_index" string="No"/>
                                <field name="lot_id" string="QR Code"/>
                                <field name="sn_vendor" string="SN Vendor"/>
                                <field name="product_id" string="Product"/>
                                <field name="picking_id" string="DO"/>
                                <field name="partner_name" string="Pelanggan"/>
                                <field name="state_reception" string="Status"/>
                                <field name="new_register" string="Status Register" domain="[('new_register', '=', 'ok')]"/>
                            </tree>
                        </field>
                    </page>
                    <page string="SN Belum Teregistrasi">
                        <field name="trip_scanned" domain="[('state_reception', 'in', ['unregistered'])]">
                            <tree create="false">
                                <field name="number_index" string="No"/>
                                <field name="qr_input"/>
                                <field name="sn_vendor" string="SN Vendor"/>
                                <field name="product_id" string="Product"/>
                                <field name="picking_id" string="DO"/>
                                <field name="partner_name" string="Pelanggan"/>
                                <field name="state_reception" string="Status" />
                                <field name="new_register" string="Status Register" domain="[('new_register','!=','old')]"/>
                            </tree>
                        </field>
                    </page>
                </notebook>

for the stock.trip model class, it's more or less like this.

class StockTrip(models.Model):
    _name = 'stock.trip'
    _description = 'Trip'
    _rec_name = 'kode_trip'
    _order = "id desc"

    trip_scanned = fields.One2many('stock.trip.sn.scanned', 'trip_id')

and model class stock.trip.sn.scanned

class StockTripScanned(models.Model):
    _name = "stock.trip.sn.scanned"
    _description = "Stock Scan Trip"

    product_id = fields.Many2one(
        'product.product', 'Product')
    trip_id = fields.Many2one('stock.trip', string="Trip")
    lot_id = fields.Many2one('stock.production.lot', 'Lot/Serial Number')
    lot_name = fields.Char(string='Lot/Serial Number')
    sn_vendor = fields.Char(string='SN Vendor', required=False, related='lot_id.sn_vendor')
    partner_name = fields.Char(string='Pelanggan', compute="compute_pelanggan")
    picking_id = fields.Many2one('stock.picking', string='Stock Picking')
    state_reception = fields.Selection([
        ('ok', 'OK'),
        ('unregistered', 'Tidak teregister'),
        ('no_in_do', 'Tidak perlu di DO'),
        ('duplicate', 'Duplikat')
    ],string="Status")
    new_register = fields.Selection([
        ('old', 'Old'),
        ('new', 'New'),
        ('none', 'None')
    ], string="new_register")
    number_index = fields.Integer(string='No', compute="_compute_no_index")
    qr_input = fields.Char(string="QR Code")
    move_line_id = fields.Many2one('stock.move.line', 'Stock Move Line')

Solution

  • I suggest to use 2 one2many fields in your model (data didn't change ,I checked) and set domain in your python file Then place each one2many fields in a single page. because domain on one2many field only worked on python file at field's definition. notice the following code:

        class StockTrip(models.Model):
        _name = 'stock.trip'
        _description = 'Trip'
        _rec_name = 'kode_trip'
        _order = "id desc"
    
        trip_scanned_new_register = fields.One2many('stock.trip.sn.scanned', 'trip_id', domain=[('new_register','=','old')])
        trip_scanned_state_reception = fields.One2many('stock.trip.sn.scanned', 'trip_id', domain=[('state_reception', 'in', ['unregistered'])])
    

    and in xml file we have :

    <page string="Scan barcode">
            <field name="trip_scanned_new_register">
                <tree create="false" edit="false">
                    <!-- any field you want to show-->
                </tree>
            </field>
        </page>
        <page string="SN Belum Teregistrasi">
            <field name="trip_scanned_state_reception" >
                <tree create="false">
                    <!-- any field you want to show-->
                </tree>
            </field>
        </page>
    </notebook>
    

    to make sure I traced it at one example and it works, I hope it works for you too :)