You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Odoo控制器中获取并展示数据库中特定城市的指定数据?

展示城市指定数据的Odoo Web控制器优化方案

看起来你已经搭建了基础的Odoo Web控制器来读取城市列表,但如果要精准展示指定数据,我们可以从几个维度调整代码,让它更贴合需求:

原代码分析

先梳理你提供的控制器代码:

from odoo import http
class CurrentWeather(http.Controller):
    @http.route('/current_weather/', auth='public', website='True')
    def index(self, **kw):
        location = http.request.env['weather_location.weather_location']
        return http.request.render('current_weather.index', {
            'locations': location.search([])
        })
    # 你提到的"/current_weather/..."部分应该是未写完的后续路由?

这段代码会把所有城市的全量数据传递给模板,但如果只需要特定字段或筛选后的城市,我们可以做以下优化:

优化方案1:仅传递指定字段数据

如果不需要模型的所有字段,用search_read()方法明确指定要获取的字段,减少不必要的数据传输:

from odoo import http
class CurrentWeather(http.Controller):
    @http.route('/current_weather/', auth='public', website=True)
    def index(self, **kw):
        location_env = http.request.env['weather_location.weather_location']
        # 按需指定要展示的字段,比如城市名、经纬度、所属省份
        locations_data = location_env.search_read(
            [],  # 空domain表示获取所有记录
            ['name', 'latitude', 'longitude', 'province_id']
        )
        return http.request.render('current_weather.index', {
            'locations': locations_data
        })

优化方案2:按条件筛选目标城市

如果只需要展示符合特定条件的城市(比如某省份的城市),可以在查询中添加domain筛选:

from odoo import http
class CurrentWeather(http.Controller):
    @http.route('/current_weather/', auth='public', website=True)
    def index(self, **kw):
        location_env = http.request.env['weather_location.weather_location']
        # 筛选出省份为"广东"的城市(假设province_id关联模型的name字段是省份名)
        locations_data = location_env.search_read(
            [('province_id.name', '=', '广东')],
            ['name', 'latitude', 'longitude']
        )
        return http.request.render('current_weather.index', {
            'locations': locations_data
        })

优化方案3:支持URL参数动态筛选

如果想要让用户通过URL参数自主指定展示范围(比如/current_weather/?province=广东),可以利用kw参数获取URL参数:

from odoo import http
class CurrentWeather(http.Controller):
    @http.route('/current_weather/', auth='public', website=True)
    def index(self, **kw):
        location_env = http.request.env['weather_location.weather_location']
        domain = []
        # 读取URL中的province参数,存在则添加筛选条件
        if kw.get('province'):
            domain.append(('province_id.name', '=', kw.get('province')))
        locations_data = location_env.search_read(
            domain,
            ['name', 'latitude', 'longitude', 'province_id']
        )
        return http.request.render('current_weather.index', {
            'locations': locations_data
        })

模板端适配提示

在对应的QWeb模板current_weather.index中,要根据数据类型访问字段:

  • 如果用search()返回记录集,用loc.name的方式访问
  • 如果用search_read()返回字典,用loc['name']的方式访问

示例模板代码:

<div class="city-list">
    <t t-foreach="locations" t-as="loc">
        <div class="city-item">
            <h3 t-esc="loc['name']"></h3>
            <p>纬度: <t t-esc="loc['latitude']"/></p>
            <p>经度: <t t-esc="loc['longitude']"/></p>
        </div>
    </t>
</div>

内容的提问来源于stack exchange,提问作者user_123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:26:29