如何在看板视图中查看One2many字段?多模型关联场景求助
在Odoo看板视图中展示关联模型字段的解决方案
嘿,我来帮你搞定这个需求!首先得确保你的两个模型关联关系是正确的,然后根据你想展示的内容(单条最新天气还是所有关联天气记录),分两种场景来实现:
第一步:确认模型关联的正确性
先把两个模型的关联字段配置对,One2many和Many2one要一一对应:
1.1 weather_location.weather_location 模型(主模型)
from odoo import models, fields, api class WeatherLocation(models.Model): _name = 'weather_location.weather_location' _description = 'Weather Location' name = fields.Char(string="City Name", required=True) # One2many关联到current_weather模型,inverse_name对应对方的Many2one字段名 current_weather_ids = fields.One2many( comodel_name='current_weather.current_weather', inverse_name='location_id', string='Current Weather Entries' )
1.2 current_weather.current_weather 模型(关联模型)
from odoo import models, fields class CurrentWeather(models.Model): _name = 'current_weather.current_weather' _description = 'Current Weather' location_id = fields.Many2one( comodel_name='weather_location.weather_location', string='Location', required=True, ondelete='cascade' ) temperature = fields.Float(string='Temperature', digits=(6,2)) humidity = fields.Float(string='Humidity', digits=(6,2)) weather_condition = fields.Char(string='Weather Condition') create_date = fields.Datetime(string='Recorded Date', readonly=True)
第二步:看板视图配置
根据你的需求选择以下两种方式:
场景1:展示最新一条天气记录
如果每个地点只需要显示最新的天气数据,建议用计算字段来获取最新记录,这样看板渲染更高效:
2.1 给主模型添加计算字段
在weather_location.weather_location里添加计算字段:
# 接上面的模型代码 latest_temperature = fields.Float( string='Latest Temperature', compute='_compute_latest_weather', digits=(6,2) ) latest_humidity = fields.Float( string='Latest Humidity', compute='_compute_latest_weather', digits=(6,2) ) latest_condition = fields.Char( string='Latest Condition', compute='_compute_latest_weather' ) @api.depends('current_weather_ids') def _compute_latest_weather(self): for rec in self: # 按创建时间倒序排序,取第一条(最新的)记录 latest_weather = rec.current_weather_ids.sorted(lambda r: r.create_date, reverse=True)[:1] rec.latest_temperature = latest_weather.temperature if latest_weather else 0.0 rec.latest_humidity = latest_weather.humidity if latest_weather else 0.0 rec.latest_condition = latest_weather.weather_condition if latest_weather else 'No Data'
2.2 配置看板视图
<record id="view_weather_location_kanban" model="ir.ui.view"> <field name="name">weather.location.kanban</field> <field name="model">weather_location.weather_location</field> <field name="arch" type="xml"> <kanban> <!-- 加载需要显示的字段 --> <field name="name"/> <field name="latest_temperature"/> <field name="latest_humidity"/> <field name="latest_condition"/> <templates> <t t-name="kanban-box"> <div class="oe_kanban_card oe_kanban_global_click"> <h3 class="oe_kanban_title"><field name="name"/></h3> <div class="oe_kanban_content"> <div class="weather-info"> <span class="label">Latest Temp:</span> <span class="value"><field name="latest_temperature"/> °C</span> </div> <div class="weather-info"> <span class="label">Humidity:</span> <span class="value"><field name="latest_humidity"/> %</span> </div> <div class="weather-info"> <span class="label">Condition:</span> <span class="value"><field name="latest_condition"/></span> </div> </div> </div> </t> </templates> </kanban> </field> </record>
场景2:展示所有关联的天气记录
如果需要在看板卡片里列出该地点的所有历史天气数据,可以直接在看板模板里循环渲染One2many字段:
<record id="view_weather_location_kanban_all" model="ir.ui.view"> <field name="name">weather.location.kanban.all</field> <field name="model">weather_location.weather_location</field> <field name="arch" type="xml"> <kanban> <!-- 必须加载One2many字段及其子字段 --> <field name="name"/> <field name="current_weather_ids"/> <field name="current_weather_ids.temperature"/> <field name="current_weather_ids.humidity"/> <field name="current_weather_ids.weather_condition"/> <field name="current_weather_ids.create_date"/> <templates> <t t-name="kanban-box"> <div class="oe_kanban_card oe_kanban_global_click"> <h3 class="oe_kanban_title"><field name="name"/></h3> <div class="oe_kanban_content"> <h4>Weather History:</h4> <!-- 循环遍历所有关联的天气记录 --> <t t-foreach="record.current_weather_ids.raw_value" t-as="weather"> <div class="weather-entry"> <span class="date"><t t-esc="weather.create_date.strftime('%Y-%m-%d %H:%M')"/></span> <span class="temp">Temp: <t t-esc="weather.temperature"/> °C</span> <span class="humidity">Humidity: <t t-esc="weather.humidity"/> %</span> <span class="condition">Condition: <t t-esc="weather.weather_condition"/></span> </div> </t> <!-- 没有记录时的提示 --> <t t-if="not record.current_weather_ids.raw_value"> <div class="no-data">No weather records found</div> </t> </div> </div> </t> </templates> </kanban> </field> </record>
注意事项
- 确保用户拥有
current_weather.current_weather模型的读取权限,否则看板里会无法加载关联字段 - 如果你的模型命名是
location.location(如你代码片段里写的),记得把上面的模型名全部替换成对应的名称,保持一致性 - 场景2如果关联记录过多,看板卡片会变得很长,建议限制显示条数(比如用切片
record.current_weather_ids.raw_value[:3]只显示前3条)
内容的提问来源于stack exchange,提问作者user_123
相关产品推荐
相关产品推荐

