如何从models.py文件中获取JS的值

时间:2018-04-26 05:18:48

标签: javascript odoo odoo-10 odoo-11

如何在models.py文件中获取JS的值。 我创建了天气信息模型,其中当前天气取自纬度和经度。我使用了openweathermap.org的api但无法获取当前位置的数据。 我使用以下代码进行编码。

.py文件

WEATHER_API_LAT_LON = "%s/weather?appid=%s&units=metric&lat={}&lon={}" % (API, APP_ID)
    @api.multi
    def get_weather_info(self,lat, lon):
        url = WEATHER_API_LAT_LON.format(lat, lon)
        print(url)

.js文件

var GetLocation = function getLocation() {
    if (navigator.geolocation) {
        return navigator.geolocation.getCurrentPosition(showPosition);
    } 
    else { 
        alert("Geolocation is not supported by this browser.");
    }
}

function showPosition(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    console.log(lat);
    console.log(lon);
}

从JS获取位置并合并到.py模型lat和lon。 任何人都可以告诉你如何做到这一点。

2 个答案:

答案 0 :(得分:0)

对于Odoo 10

* model.py

@api.multi
def get_weather_info(self,lat, lon):
    url = WEATHER_API_LAT_LON.format(lat, lon)
    print(url)
    return url

*。JS

// on header
var Model = require('web.Model');
// inside main function
var model  = new Model('model.name').call('get_weather_info',[]).then(function(result){
    console.log(result)
});

对于Odoo 11

*。JS

var self = this;
self._rpc({
    model: 'model.name',
    method: 'get_weather_info',
}, []).then(function(result){
    console.log(result)
})

在调用model.py

中的方法时,在数组中传递lat和lon

答案 1 :(得分:0)

<强> *。model.py

@api.multi
def get_weather_info(self,lat, lon):
    url = WEATHER_API_LAT_LON.format(lat, lon)
    print(url)

*。js for odoo 11

var self = this;
self._rpc({
    model: 'model.name',
    method: 'get_weather_info',
    args: [], //Here you have to pass the arguments using keyword agrs.
}).then(function(result){
    console.log(result)
})
相关问题