I want to get the list of the character fields from another model depending upon the matching of one field between 2 models, I wrote the code, the data is coming in the logs, but isn't getting assigned to the Selection field, can anyone help me with it?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Бухгалтерия
- Склад
- PoS
- Project
- MRP
Этот вопрос был отмечен
Hi,
Please refer to the code:
from odoo import models, fields, api
class YourModel(models.Model):
_name = 'your.model'
account_id = fields.Many2one(
'account.account',
string='Fixed Asset Account',
help="Select the fixed asset account."
)
capacity_form = fields.Selection(
selection=lambda self: self._get_capacity_selection(),
string='Capacity',
help="Select the capacity for the selected fixed asset account."
)
@api.model
def _get_capacity_selection(self):
# Default empty if no context
account_id = self.env.context.get('default_account_id')
if not account_id:
return []
asset_records = self.env['account.asset'].search([
('account_asset_id', '=', account_id)
])
return [(asset.capacity, asset.capacity) for asset in asset_records if asset.capacity]
Since selection fields are static per field definition, the usual trick is to use a Many2one instead of a Selection. You can then dynamically restrict values with a domain:
from odoo import models, fields, api
class YourModel(models.Model):
_name = 'your.model'
account_id = fields.Many2one(
'account.account',
string='Fixed Asset Account',
help="Select the fixed asset account."
)
capacity_form_ids = fields.Many2many(
'account.asset',
compute='_compute_capacity_form_ids',
string='Available Capacities'
)
capacity_form_id = fields.Many2one(
'account.asset',
string='Capacity',
domain="[('id', 'in', capacity_form_ids)]"
)
@api.depends('account_id')
def _compute_capacity_form_ids(self):
for rec in self:
if rec.account_id:
assets = self.env['account.asset'].search([
('account_asset_id', '=', rec.account_id.id),
('capacity', '!=', False)
])
rec.capacity_form_ids = assets
else:
rec.capacity_form_ids = False
Hope it helps.
Following is the code @Ray Carnes
account_id = fields.Many2one(
'account.account',
string='Fixed Asset Account',
help="Select the fixed asset account."
)
capacity_form = fields.Selection(
selection=[],
string='Capacity',
help="Select the capacity for the selected fixed asset account."
)
@api.onchange('account_id')
def _onchange_account_id(self):
if self.account_id:
# Fetch all capacities related to the selected account
asset_records = self.env['account.asset'].search([
('account_asset_id', '=', self.account_id.id)
])
# Prepare the selection list
capacities = [(asset.capacity, asset.capacity) for asset in asset_records if asset.capacity]
print('Capacities are', capacities)
# Dynamically update the selection options for capacity_form
self._fields['capacity_form'].selection = capacities
self.capacity_form = capacities
# Reset the value of capacity_form if it doesn't match the new options
if self.capacity_form not in dict(capacities):
self.capacity_form = False
else:
# Clear the selection if no account_id is selected
self._fields['capacity_form'].selection = []
self.capacity_form = False
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
РегистрацияRelated Posts | Ответы | Просмотры | Активность | |
---|---|---|---|---|
Blank Page After Logged in.
Решено
|
|
2
сент. 25
|
3639 | |
|
2
нояб. 24
|
3411 | ||
|
1
сент. 23
|
5159 | ||
|
0
апр. 21
|
3725 | ||
|
2
июн. 25
|
2259 |
Your question is very theoretical. Can you post your code or something specific? Where would readers start if they wanted to help?