如何获取特定城市的具体数据?

时间:2018-03-28 07:48:09

标签: python controller odoo odoo-11

我有数据库中的城市列表,并希望在Web控制器中显示其特定数据。 在这里,附上截图和我使用的代码。

Controllerrs.py文件

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([])
            })

    @http.route('/current_weather/condition', auth='public', website='True')
    def index_current_condition(self, **kw):
        weather_condition = http.request.env['current_weather.current_weather']
        return http.request.render('current_weather.index_condition', {
            'weather_conditions': weather_condition.search([])
            })

Template.xml文件

        <template id="index">
        <t t-call="website.layout">
            <t t-set="title">Locations</t>
            <div class="oe_structure">
                <div class="container">
                    <t t-foreach="locations" t-as="location">
                        <p>
                            <a t-attf-href="/current_weather/condition">
                            <center><t t-esc="location.name"/></center>
                            </a>
                        </p>
                    </t>

                </div>
            </div>
        </t>
    </template>

enter image description here 点击孟买时,只有孟买的数据会显示如何做到这一点? 目前,所有城市的数据都是相同的。

1 个答案:

答案 0 :(得分:0)

在您的控制器index_current_condition中,您需要考虑根据点击的城市过滤weather_condition的方法。这可以通过search method of the ORM API完成。

使用weather_condition.search([]),当您传递空白列表时,您将返回模型中找到的所有记录。所以在这里你需要使用weather_condition.search([('city', '=', current_city)])的内容,其中city需要是模型current_weather.current_weather中的一个字段(并且应该链接到您的其他模型weather_location.weather_location并且current_city必须以某种方式动态传递给控制器​​。