如何使用Odoo从表单字段获取值? [英] How do I get the value from a form field using Odoo?

查看:153
本文介绍了如何使用Odoo从表单字段获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单视图中有此字段:

I have this field in a form view:

<field name="value"/>

当有人想要添加新值时,我想从字段中获取值,这是我在PHP中执行$_GET['value']的方式.

I want to get the value from the field when someone wants to add a new value, the way that I might do $_GET['value'] in PHP.

简单的例子:

我希望在用户插入一个值时,让程序检查该值是否大于所有值的总和并显示一条错误消息,例如:

I want, when the user inserts a value, for the program to check if it is greater than sum of all values and print an error message like:

无法添加该值,因为它大于所有值的总和

Cannot add the value because it is greater than the sum of all values

到目前为止,我已经写了这篇文章:

I've written this so far:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
    <record id="action_test" model="ir.actions.act_window">
            <field name="name">Insert</field>
            <field name="res_model">test.odoo</field>
            <field name="view_mode">form,tree</field>
    </record>

    <record id="view_tree_test" model="ir.ui.view">
        <field name="name">Inset.tree</field>
        <field name="model">test.odoo</field>
        <field name="arch" type="xml">
            <tree string="T">
                <field name="create_uid"/>
                <field name="value" sum="Total Sum"/>
            </tree>
        </field>
    </record>

    <record id="view_from_test" model="ir.ui.view">
        <field name="name">Inset.form</field>
        <field name="model">test.odoo</field>
        <field name="arch" type="xml">
            <form string="T_Form">
                <group>
                    <field name="value"/>
                </group>
            </form>
        </field>
    </record>


    <menuitem name="Test Module" id="main_menu_test" sequence="3"/>

    <menuitem name="TEST" id="sub_menu" parent="main_menu_test"/>

    <menuitem action="action_test" id="action_menu" parent="sub_menu" />
</data>
</openerp>

test.py

from openerp import models
from openerp import fields

class test(models.Model):
    _name = "test.odoo"
    value = fields.Integer()

推荐答案

您无法独立于数据库层来考虑odoo中的表单视图.

You cannot think about form view in odoo in isolation from the database layer.

表单中的数据立即(自动)保存在数据库中.您以后可以使用 ORM方法访问它

The data from the form is immediately (and automatically) saved in the database. You can later access it using the ORM methods.

我不知道您要实现什么目标,因此我很难为您提供一个具体的例子.每个表单视图都与单个ORM模型相关联.如果要在保存数据之前/之后立即对数据进行处理,通常可以将ORM模型作为子类并覆盖其中的一种方法.

I don't know what exactly you want to achieve, so it's hard for me to give you a concrete example. Every form view is associated with a single ORM model. If you want to do do something with the data immediately before/after it is saved, you would usually subclass the ORM model and overwrite one of its methods.

class Foo(models.Model):
    _inherit = 'other.foo'

    @api.model
    def create(self, vals):
        record = super(Foo, self).create(vals)
        print "A new Foo with name={} and bar={} has been created!".format(
            record.name,
            record.bar,
        )
        return record

这是验证表单的方式:

from openerp import models, fields, api, exceptions

class test(models.Model):
    _name = 'test.odoo'
    value = fields.Integer()

    @api.one
    @api.constrains('value')
    def _check_value(self):
    if self.value > 25:
        raise exceptions.ValidationError("The value is too large!")

这篇关于如何使用Odoo从表单字段获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆