I want to change the retrieved set of values of a many2many field according to a many2one field onchange function, it worked well with another many2one field, but doesn't seem to filter the outcome on a many2many field
my code is as follows
class CustomPurchase(models.Model):
_name = 'custom.purchase'
_description = 'Purchase Record'
supplier_id = fields.Many2one('custom.supplier', string='Supplier', required=True)
product_ids = fields.Many2many('custom.supply.line', string='Purchase Lines', required=True)
#api.onchange('supplier_id')
def onchange_supplier(self):
selected_lines = []
if self.supplier_id:
for rec in self:
selected_lines = rec.env['custom.supply.line'].search([('supplier_id', '=', rec.supplier_id.id)])
domain = {'product_ids': [('id', '=', selected_lines.id)]}
return {'domain': domain, 'value': {'selected_lines': []}}
Expected behavior is to have just the items related to the supplier_id many2one field
Produced behavior is all the items are retrieved
Edit:
I've noticed that i when i remove widget="section_and_note_one2many" the domain works perfectly, yet the tree view can't be edited within the same form, even with editable="bottom"
You can add check using supplier field directly inside domain.
Code:
#api.onchange('supplier_id')
def onchange_supplier(self):
domain = {'product_ids': []}
for rec in self:
if rec.supplier_id:
domain = {'product_ids': [('supplier_id', '=', rec.supplier_id.id)]}
return {'domain': domain}
UPDATE:
Try to implement this as same as in /addons/mrp/models/mrp_bom.py on attribute_value_ids. [Version:12]
Related
I have a field "qty_av" which i set dynamically via an onchange method depending on field "branch_product_ids" value, the "qty_av" is successfully set as long as i haven't saved the record yet
But once the record is saved "qty_av" is turned into null
Before:
After:
My code
branch_product_ids = fields.Many2one(comodel_name="custom.product", string="Product",
domain="[('branch_line.branch_id.user_lines.user_id','=', user_id)]")
user_id = fields.Many2one('res.users', 'Current User', default=lambda self: self.env.user.id)
branch_id = fields.Many2one('custom.branch', string="Branch", required=False, compute="_get_branch", store=True)
qty_av = fields.Integer('Av Qty', readonly=True, store=True)
#api.onchange('branch_product_ids')
def onchange_product(self):
selected_lines = []
if self.branch_product_ids:
for rec in self:
selected_lines = rec.env['custom.branch.line'].search(
['&', ('product_id', '=', rec.branch_product_ids.id), ('branch_id', '=', rec.branch_id.id)]).mapped(
'qty')
if not selected_lines:
raise ValidationError(
_('Current branch has ZERO stock on this product, Please select an available Product'))
else:
rec.qty_av = selected_lines[0]
Because you are making it read-only that is why.
To store read-only field try this.
In Python:
qty_av = fields.Integer('Av Qty')
In XML:
<field name="qty_av" readonly="1" force_save="1"/>
Imagine you want to fill a field in the sale.order form with a default value, the same text will be used for each company that is being used in Odoo. Sometimes the usual way to proceed is to use a common field in the res.company model. Other option is to add a field that is filled with some other content in the res.partner form, when the customer is selected.
But the problem I found is: if I want to get the translations for that field, which are already in the original one, I would have to do it manually, inheriting the create method of ir.translation. When the field is copied to the sale.order form a record for the current used language is created, so I took advantage of it to create the rest of records. Is there a better way to do this?
Also, I would like to add that after saving the record I press the world icon to translate the text again, new translations are created with the current text. So I would need to translate everything again on every sale.order record.
I did this in the ir.translation model for a simple text field in order to create the translations of the rest of languages. The field field_to_translate is translatable on both sides:
#api.model
def create(self, vals):
record = super(IrTranslation, self).create(vals)
name = vals.get('name', False) # name of the field to translate
lang = vals.get('lang', False) # creating record for this language
if 'context' in dir(self.env):
cur_lang = self.env.context.get('lang', False) # current used language
if name == 'sale.order,field_to_translate' and lang == cur_lang:
langs = self.env['ir.translation']._get_languages()
langs = [l[0] for l in langs if l[0] != cur_lang] # installed languages
for l in langs:
if self.env.user.company_id.field_to_translate:
t = self.env['ir.translation'].search([
('lang', '=', l),
('type', '=', 'model'),
('name', '=', 'res.company,field_to_translate')
])
if t:
self.env['ir.translation'].create({
'lang': l,
'type': 'model',
'name': 'sale.order,field_to_translate',
'res_id': record.res_id,
'src': record.src,
'value': t.value,
'state': 'translated',
})
Ah, and I want to do this because I want to print them on different reports. The language of these report will depend on the customer lang field.
In short, how can I set a translatable field in res.company as a default value in other field in the sale.order model? The translations to other languages should be copied as well. My above proposal is kind of cumbersome
Well, finally I have created the translations in the create method of sale.order. I have used a customized translate_fields method of the ir.translation model in order to create the translations automatically and then I update all their values manually with the correct language translation.
On the other hand I found this class in the l10n_multilang module that can be useful for other people that may find the same problem:
class AccountChartTemplate(models.Model):
_inherit = 'account.chart.template'
#api.multi
def process_translations(self, langs, in_field, in_ids, out_ids):
"""
This method copies translations values of templates into new Accounts/Taxes/Journals for languages selected
:param langs: List of languages to load for new records
:param in_field: Name of the translatable field of source templates
:param in_ids: Recordset of ids of source object
:param out_ids: Recordset of ids of destination object
:return: True
"""
xlat_obj = self.env['ir.translation']
#find the source from Account Template
for lang in langs:
#find the value from Translation
value = xlat_obj._get_ids(in_ids._name + ',' + in_field, 'model', lang, in_ids.ids)
counter = 0
for element in in_ids.with_context(lang=None):
if value[element.id]:
#copy Translation from Source to Destination object
xlat_obj._set_ids(
out_ids._name + ',' + in_field,
'model',
lang,
out_ids[counter].ids,
value[element.id],
element[in_field]
)
else:
_logger.info('Language: %s. Translation from template: there is no translation available for %s!' % (lang, element[in_field]))
counter += 1
return True
#api.multi
def process_coa_translations(self):
installed_langs = dict(self.env['res.lang'].get_installed())
company_obj = self.env['res.company']
for chart_template_id in self:
langs = []
if chart_template_id.spoken_languages:
for lang in chart_template_id.spoken_languages.split(';'):
if lang not in installed_langs:
# the language is not installed, so we don't need to load its translations
continue
else:
langs.append(lang)
if langs:
company_ids = company_obj.search([('chart_template_id', '=', chart_template_id.id)])
for company in company_ids:
# write account.account translations in the real COA
chart_template_id._process_accounts_translations(company.id, langs, 'name')
# copy account.tax name translations
chart_template_id._process_taxes_translations(company.id, langs, 'name')
# copy account.tax description translations
chart_template_id._process_taxes_translations(company.id, langs, 'description')
# copy account.fiscal.position translations
chart_template_id._process_fiscal_pos_translations(company.id, langs, 'name')
return True
#api.multi
def _process_accounts_translations(self, company_id, langs, field):
in_ids, out_ids = self._get_template_from_model(company_id, 'account.account')
return self.process_translations(langs, field, in_ids, out_ids)
#api.multi
def _process_taxes_translations(self, company_id, langs, field):
in_ids, out_ids = self._get_template_from_model(company_id, 'account.tax')
return self.process_translations(langs, field, in_ids, out_ids)
#api.multi
def _process_fiscal_pos_translations(self, company_id, langs, field):
in_ids, out_ids = self._get_template_from_model(company_id, 'account.fiscal.position')
return self.process_translations(langs, field, in_ids, out_ids)
def _get_template_from_model(self, company_id, model):
out_data = self.env['ir.model.data'].search([('model', '=', model), ('name', '=like', str(company_id)+'\_%')])
out_ids = self.env[model].search([('id', 'in', out_data.mapped('res_id'))], order='id')
in_xml_id_names = [xml_id.partition(str(company_id) + '_')[-1] for xml_id in out_data.mapped('name')]
in_xml_ids = self.env['ir.model.data'].search([('model', '=', model+'.template'), ('name', 'in', in_xml_id_names)])
in_ids = self.env[model+'.template'].search([('id', 'in', in_xml_ids.mapped('res_id'))], order='id')
return (in_ids, out_ids)
I have these fields in my model:
seller = fields.Many2one('res.partner', string="Select Seller",domain="[('supplier','=',True)]")
products= fields.Many2one('product.template', string="Select Product" )
Now, i need to filter the second field as the user chooses a seller(first field)
How do i set a domain onchange.
I am trying to do something like,
#api.onchange('seller')
def onchange_field_seller(self):
res = {}
if self.seller:
# return {'domain':{'product':[//what do i add here//]}}
return res
I am using a many2many field to create the products in the seller creating form.
product_details = fields.Many2many('product.template',string="Products")
(Please note that the form with this field is different from the one in the question above).
I am trying to get only those product entries that were created when i created the seller entry.Im really confused, how do i accomplish this?
In your case you can't use a "dynamic" domain but more of a pre-defined domain on product IDs.
#api.onchange('seller')
def onchange_field_seller(self):
if self.seller:
# filter products by seller
product_ids = self.seller.product_details.ids
return {'domain': {'product': [('id', 'in', product_ids)]}}
else:
# filter all products -> remove domain
return {'domain': {'product': []}}
I tried to use api.constrain in odoo. The case is, I want to give condition to odoo if user missing one field, odoo will give some warning like alert, and user have to give some input to the previous field. This is my code :
class Provinc(models.Model):
_name = 'provinsi.model'
_rec_name ='prov'
prov = fields.Char(
string='Provinsi',
)
res_partner_ids = fields.One2many(
'res.partner',
'provinsi'
city_id = fields.One2many(
'city.model',
'provinsi_id'
)
class city(models.Model):
_name = 'kota.model'
_rec_name ='city'
city = fields.Char(
string='City',
)
res_partner_city = fields.One2many(
'res.partner',
'city'
)
provinsi_id = fields.Many2one(
'provinsi.model',
string='provinsi',
)
class Master_data(models.Model):
_inherit = 'res.partner'
provinsi = fields.Many2one(
'provinsi.model',
'provinsi',
)
city = fields.Many2one(
'city.model',
'city',
)
#api.onchange('provinsi')
def show_provinsi(self):
return {'domain': {'city': [('provinsi_id', '=', self.provinsi.id)]}}
#api.constrains('provinsi')
#api.onchange('city')
def show_kota(self):
for record in self:
if record.provinsi == False:
raise ValidationError("is gonna be error")
I have tried 2 ways. First, I put the api.constrain insode class City, and it's doesn't work,second ways I tried put the api.constrain like the code inside class master, and the result remains the same.
you only need onchang not any constrains to display warning!
#api.onchange('city')
def show_kota(self):
if self.city and not self.provinsi:
raise ValidationError("is gonna be error")
now if you select value in city field and provinsi missing value then it will display error msg
guessing field "city" and "provinsi" is in 'res.partner' form view!
If you want to check a field value is empty or not, just use required ="1" like:
<field name="provinsi" required="1"/>
Below code is asset inherited class . Here i will add 'place' field with 'Karn/Bang/Kengeri' and 'karn/bang/malleshwaram' for 'Karn/Bang/Kengeri' will add 'asset_catg_id' with A and B. then for 'karn/bang/malleshwaram' with Y and Z.
Now at calander inherited class . if i select 'place' with 'Karn/Bang/Kengeri' then next field 'asset_catg_id' i have to get only A and B drop down list. if again 'karn/bang/malleshwaram' then i have to get only Y,Z options . and previous selected 'asset_catg_id' values should get deleted . i have tried with domain filter option got keyvalue error
class asset_asset(osv.osv):
_inherit = "asset.asset"
#_name = "asset_asset"
_rec_name= "folio_num"
_columns = {
'name': fields.char('Asset Name', size=64),
'place': fields.many2one('asset.parentlocation', 'Location'),
'asset_catg_id' : fields.many2one('asset.catg', 'Asset Catg Selection', select=True, required=True),}
class asset_catg(osv.Model):
_name="asset.catg"
_rec_name='name1'
_description="Define Asset Catgs"
_columns={ 'name1':fields.char('Asset Catg Names',size=64,required=True),}
asset_catg()
class asset_parentlocation(osv.osv):
_name="asset.parentlocation"
_rec_name="location_name"
_columns = {
'location_name' : fields.char('Asset Location', required=True),
'parent_location' : fields.many2one('asset.parentlocation','Parent Location'),
'nameee':fields.many2one('ir.attachment','Attachments'),}
def name_get(self, cr, uid, ids, context=None):
if context is None:
context = {}
if not ids:
return []
reads = self.read(cr, uid, ids, ['location_name','parent_location'], context=context)
res = []
for record in reads:
name = record['location_name']
if record['parent_location']:
name = record['parent_location'][1]+' / '+name
res.append((record['id'], name))
return res
**Following code is calendar inherited class**
class calendar_event(osv.osv):
_inherit = "calendar.event"
_rec_name = 'number'
_columns = {
'number' : fields.char('Meeting ID',readonly=1),
#'place' : fields.many2one('stock.location','Substation Location',),
'place' : fields.many2one('asset.parentlocation','Substation Location',),
#'location' : fields.selection(STATUS_SELECTION,'Location', default='Board A'),
'asset_catg_id' : fields.many2one('asset.catg','Asset Catg Selection', domain="[('asset_catg_id', '=',place)]"),}
First your domain is wrong in a principle. Domain is what is "inside" a field, in other words in its model (for example field name or id in asset.catg model). So you should fix that one first.
If domain depends on another field, you can use onchange method to return domain (used placeholder place_id). Like this:
#api.onchange('place')
def onchange_place(self):
res = {}
if self.place:
res['domain'] = {'asset_catg_id': [('place_id', '=', self.place.id)]}
return res
P.S. This is example with new v8 api, but same principle applies to old api (you then don't nee to use decorator, also add cr, uid, ids on method and call it through your view. All of this not needed for new api). As it looks like you are still developing on old api.
Update
For old api:
def onchange_place(self,cr, uid, ids, place, context=None):
res = {}
if self.place: #on old api it will return id, instead of record
res['domain'] = {'asset_catg_id': [('place_id', '=', self.place)]}
return res
And then in your view (don't know what kind of view you are using):
<field name="place" on_change="onchange_place(place)"/>
Still you need to define some field in asset.catg so it would be used to match place field. For example:
'place_id': fields.many2one('asset.parentlocation', 'Place')
And then when you define asset category, you set which Place it should belong to. Then when you choose place calendar.event, onchange method will set domain on asset_catg_id field correctly.