21433

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

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

    

Answer1:

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

    

Recommend

  • Diplay different kanban view than default
  • How do I properly create, write or unlink records for many2many field using the new API?
  • keyError, When import data in openerp
  • DNN Module Import file
  • How to remove Save Button from form view
  • Nested Tree View in Odoo v8
  • Product and product template in Odoo 10
  • RFX equivalent data type for _int64 in Informix
  • Add a TCombobox Column to a Firemonkey TGrid
  • Making turtles wait x number of ticks
  • Selecting TOP 4 records from multiple SQL Server tables. Using vb.net
  • How can i filter mysql data if a column has multiple comma separated values?
  • Nanoseconds lost coming from MongoDB ISODate Object
  • How can I sum two different columns at once where one contains Decimal objects in pandas?
  • PHP mail() function not delivering mail
  • Select count of rows that have a certain number of rows in a related table
  • Update SQL MS Access 2010
  • Set the default timezone in symfony
  • TSQL Rolling Average of Time Groupings
  • Changing references to deprecated methods C++
  • SQL - Select lowest values with group by and order by?
  • Configure nginx to return different files to different authenticated users with the same URI
  • Copy to all folders batch file?
  • Excel's Macro-Recorder usage
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • Marklogic : Query response time is very high
  • Why querying a date BC is changed to AD in Java?
  • Record samples being played with OpenAL
  • D3 nodes and links from JSON with nested arrays of children
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • PHP - How to update data to MySQL when click a radio button
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?