【发布时间】:2018-03-27 06:51:02
【问题描述】:
我有两个模型 weather_location.weather_location 和 current_weather.current_weather。 weather_location.weather_location 有 One2many 字段,current_weather.current_weather 有 Many2one 字段。现在,我想在 weather_location.weather_location 的看板视图中显示在 current_weather.current_weather 中声明的字段。 在这里,附上我尝试过的代码:
class location(models.Model):
_name = 'location.location'
name = fields.Char(string="City Name")
location_ids = fields.One2many('weather.weather',
'weather_id', string="Weather Details")
api_address = "http://api.openweathermap.org/data/2.5/weather?appid=6784429f5eff661309aaa5280a143f97&q="
def get_weather_data(self):
url = self.api_address + self.name
json_data = requests.get(url).json()
formatted_data = json_data['weather'][0]['main']
formatted_data1 = json_data['main']['temp']
formatted_data2 = json_data['main']['temp_min']
formatted_data3 = json_data['main']['temp_max']
formatted_data4 = json_data['main']['humidity']
self.env['weather.weather'].create(
{'weather_id':self.id,
'main':formatted_data,
'temp':formatted_data1,
'temp_min':formatted_data2,
'temp_max':formatted_data3,
'humidity':formatted_data4,
})
class current_weather(models.Model):
_name = 'weather.weather'
weather_id = fields.Many2one('location.location',
string="Weather",)
color = fields.Integer()
main = fields.Char(string="Main")
temp = fields.Float(string="Temperature")
temp_min = fields.Float(string="Minimum Temperature")
temp_max = fields.Float(string="Maximum Temperature")
humidity = fields.Float(string="Humidity")
.xml 文件(看板视图):
<record model="ir.ui.view" id="current_location_Kanban">
<field name="name">current_weather kanban</field>
<field name="model">location.location</field>
<field name="type">kanban</field>
<field name="arch" type="xml">
<kanban default_group_by="name">
<field name="name"/>
<templates>
<t t-name="kanban-box">
<div t-attf-class="oe_kanban_content">
Main:
<!-- <field name="main"/> -->
<br/>
Temperature:
<!-- <field name="temp"/> -->
<br/>
Minimum Temperature:
<!-- <field name="temp_min"/> -->
<br/>
Maximum Temperature
<!-- <field name="temp_max"/> -->
<br/>
Humidity:
<!-- <field name="humidity"/> -->
</div>
</t>
</templates>
</kanban>
</field>
</record>
通过使用它只显示标签而不显示它们的值。如何访问它们的值
【问题讨论】:
-
显示该看板视图的屏幕截图@user_123
-
检查已编辑的。 @Naveen
-
删除字段@user_123的cmets
-
如果删除则给出错误(即 field_name 不存在)@Naveen
-
显示weather_location.weather_location模型@user_123