python - selecting only the available data on Many2one field -
i've created module asset.management.handover
, asset.management.handover.lines
save selected assets it.
i done on filtering data on many2one field below. here .py code:
asset_name_id = fields.many2one('account.asset.asset', string = "asset", required = true) serial_number_id = fields.many2one('account.asset.asset.line', string = "serial number", required = true)
my .xml code:
<field name="asset_name_id"/> <field name="serial_number_id" domain = "[('serial_no_id', '=', asset_name_id)]"/>
if select asset1, available serial no. asset1 can selected. problem is:
i want to, if, example, select asset1 select serial no. 123 want select item asset1 again when want select serial no. again, there goes problem, serial no. 123 available again. want achieve is, serial no. 123 no longer available many2one , cannot seen field.
please click link view picture
(edited) full code of .py
from odoo import api, fields, models datetime import datetime class assetmanagementhandover (models.model): _name = 'asset.management.handover' name = fields.char(string = "sequence") issuer_company_id = fields.many2one ('res.partner', string = "issuer's company") issuer_id = fields.many2one ('hr.employee', string = "issuer's name") issuer_email = fields.char (string = "email", readonly = true, related = 'issuer_id.user_id.login', store = true) source_loc = fields.many2one ('stock.warehouse', string = "source location") recipient_company_id = fields.many2one ('res.partner', string = "recipient's company") recipient_id = fields.many2one ('hr.employee', string = "recipient's name") destination_loc = fields.many2one ('stock.warehouse', string = "destination location") remarks = fields.text (string = "remarks") date = fields.date (string = "date", default = lambda *a: datetime.today()) transfer_type = fields.char (string = "transfer type", default = "asset handover", readonly = true) custodian_id = fields.many2one ('res.users', string = "custodian", readonly = true, default=lambda self: self.env.uid) processed_by = fields.many2one ('hr.employee', string = "processed by", readonly = true) lines_ids = fields.one2many('asset.management.handover.lines', 'lines_id', string = " ") state = fields.selection ([ ('draft', "draft"), ('transfer', "transferred"), ('cancel', "cancelled"), ], string = "state", default = 'draft') @api.onchange('issuer_company_id') def onchange_company(self): self.issuer_id = '' self.emp_email = '' self.source_loc = '' @api.multi def button_transfer(self): self.state = 'transfer' self.processed_by = self.env['hr.employee'].browse(self.env.uid) @api.multi def button_cancel(self): self.state = 'cancel' self.processed_by = '' class assetmanagementhandoverline (models.model): _name = 'asset.management.handover.lines' lines_id = fields.many2one('asset.management.handover') asset_name_id = fields.many2one('account.asset.asset', string = "asset", required = true) serial_number_id = fields.many2one('account.asset.asset.line', string = "serial number", required = true) model = fields.char (string = "model", related = 'asset_name_id.model_id', store = true, readonly = true) condition_id = fields.many2one ('asset.condition', string = "asset condition", required = true) state = fields.char (string = "state") asset_pic = fields.char(string = "asset picture") total = fields.integer(string = "total", default = "1") @api.onchange('asset_name_id') def on_change(self): self.serial_number_id = ''
this form view of asset.management.handover in xml
<!--hand on form view--> <record model="ir.ui.view" id="handover_form_view"> <field name="name">handover.form.view</field> <field name="model">asset.management.handover</field> <field name="arch" type="xml"> <form> <header> <button name="button_transfer" string="transfer" type="object" state="cancel"/> <button name="button_cancel" string="cancel" type="object"/> <field name="state" widget="statusbar"/> </header> <sheet> <group> <group> <field name="date"/> <field name="issuer_company_id" placeholder="company name" domain="[('is_company','=',true)]"/> <field name="issuer_id" placeholder="employee's name" domain = "[('address_id', '=', issuer_company_id)]"/> <field name="source_loc" domain = " [('partner_id', '=', issuer_company_id)]"/> <field name="issuer_email"/> <field name="remarks"/> </group> <group> <field name="transfer_type"/> <field name="recipient_company_id" placeholder="company name" domain="[('is_company','=',true)]"/> <field name="recipient_id" placeholder="employee's name" domain = "[('address_id', '=', recipient_company_id)]"/> <field name="destination_loc" domain = " [('partner_id', '=', recipient_company_id)]"/> <field name="custodian_id"/> <field name="processed_by"/> </group> <field name="lines_ids"> <tree editable="bottom"> <field name="asset_name_id"/> <field name="serial_number_id" domain = " [('serial_no_id', '=', asset_name_id)]"/> <field name="model"/> <field name="condition_id"/> <field name="state"/> <field name="asset_pic"/> <field name="total" sum="total" string = "total asset"/> </tree> <form> <sheet> <group> <group> <field name="asset_name_id"/> <field name="serial_number_id" domain = "[('serial_no_id', '=', asset_name_id)]"/> <field name="model"/> <field name="condition_id"/> </group> <group> <field name="state"/> <field name="asset_pic"/> </group> </group> </sheet> </form> </field> </group> </sheet> </form> </field> </record>
a simple way have values checked on write
or create
methods. , if serial exists on more 1 products raise exception.
another way set dynamic domain. can example following:
when field1
changes, want able select these records on field2
so, define onchange
, return
return {'domain': {'field_id': [('id', 'not in', used_serials)]}}
where on used_serials
have ids of serials used.
wiki
Comments
Post a Comment