How do I get the value from a form field using Odoo?

不问归期 提交于 2019-12-11 03:38:35

问题


I have this field in a form view:

<field name="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.

Simple example:

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:

view.xml

<?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()

回答1:


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

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

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

This is how you validate a form:

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!")


来源:https://stackoverflow.com/questions/27123368/how-do-i-get-the-value-from-a-form-field-using-odoo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!