简体   繁体   English

Odoo 12:如何在@ api.model方法中使用表单值

[英]Odoo 12: How to use form values in @api.model methods

I am new in the forum. 我是论坛的新人。
I am currently working with Odoo 12 and I'm having some problems communicating Odoo Model with my Javascript Widget. 我目前正在使用Odoo 12,我在使用我的Javascript Widget与Odoo模型进行通信时遇到了一些问题。 Step by step... 一步步...
1. I am creating a widget that shows some Google Charts and the only way I've found was to show the QWeb code in a custom view and assign the Widget to a Char field. 1.我正在创建一个显示一些Google Charts的小部件,我发现的唯一方法是在自定义视图中显示QWeb代码并将Widget分配给Char字段。
2. I've achieved a nice approach using rpc in my start: function using rpc and getting the data from the module. 2.我在开始时使用rpc实现了一个很好的方法:使用rpc的函数并从模块中获取数据。
3. THE PROBLEM: 3.问题:
I need to show two Date fields in the form and use that dates as an interval for my Google Charts: How can I get the date values in my @api.model 我需要在表单中显示两个日期字段,并将该日期用作我的Google图表的间隔:如何在@ api.model中获取日期值
function?? 功能??
The function can only read a resultset of all the table, when I try to get the result (self.init_date) set in the field, I always receive "False" 该函数只能读取所有表的结果集,当我尝试在字段中设置结果(self.init_date)时,我总是收到“False”
Sorry, I can't post the code today, but on Monday I will post it if nobody can give me the answer. 对不起,我今天无法发布代码,但周一我会发帖,如果没有人能给我答案的话。
Thank you very much! 非常感谢你!

Hi Try to use something like this 嗨尝试使用这样的东西

from odoo import api,models, fields, _

class MyCustomModel(models.Model):
    _name = "my.custom.model"

    #... some fields
    date1 =  fields.Date(string="Date 1")
    date2 =  fields.Date(string="Date 2")

    @api.model
    def my_custom_func(self):
        # read all data in table
        all_records = self.env['my.custom.model'].search([])
        for rec in all_records:
            print(rec.date1, rec.date2)

this a simple example to access to date1 and date2 for all records 这是一个访问所有记录的date1和date2的简单示例

The api.model decorator gives you access to the model's methods but not to the records, so they can be called when the model has no records (See https://www.odoo.com/documentation/12.0/reference/orm.html#module-odoo.api ). api.model装饰api.model您可以访问模型的方法,但不能访问记录,因此可以在模型没有记录时调用它们(请参阅https://www.odoo.com/documentation/12.0/reference/orm.html #module-odoo.api )。

If the method you wrote is intended to always receive a record, just remove the api.model decorator. 如果您编写的方法总是要接收记录,只需删除api.model装饰器即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM