Welcome!

Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

Sign up

You need to be registered to interact with the community.
This question has been flagged
1 Reply
15 Views

Hey Cyllo folks, Anyone know how to make a custom integer field widget? I'm trying to add some extra functionality, and I'm stuck. Pointers appreciated!

Avatar
Discard

Here's how you can implement a custom widget in Odoo that converts float values to integers. This solution covers the backend model, frontend OWL widget, and QWeb template.

This example demonstrates a custom Odoo widget that converts float values to integers. It involves extending the 'res.partner' model, creating a custom OWL widget, and designing a QWeb template for rendering.

from odoo import fields, models

class ResPartner(models.Model):
      _inherit = "res.partner"
      float_int = fields.Char(string="Float to Int")

Views:

This XML view inheritance adds the float_int field to the partner form, positioned after category_id, using a custom float_int_widget for display.


   

        res.partner.view.form.inherit.float.int.widget

        res.partner

       

       

           

               

           

       

   



Frontend: OWL

/** @odoo-module **/

import { registry } from "@web/core/registry";
import { useInputField } from "@web/views/fields/input_field_hook";
import { standardFieldProps } from "@web/views/fields/standard_field_props";
import { onRendered } from "@odoo/owl";
import {Component, useState} from "@odoo/owl";
import { useRef, useEffect, onWillRender, onMounted } from "@odoo/owl";
import { parseFloat } from "@web/views/fields/parsers";


export class FloatInt extends Component{
​static template = "float_int_widget.FloatIntField";
  ​setup(){
  ​this.input = useRef('inputfloatint')
  ​useInputField({ getValue: () => this.props.record.data[this.props.name] || "" ,
  ​refName: "inputfloatint",parse: (v) => parseFloat(v),}); ​onWillRender(() => {
  ​if (this.input.el) {
  ​this.props.record.data[this.props.name] =  Math.round(this.input.el.value) } } 

​});});
  ​onMounted(() => {
  ​if (this.input.el) {
  ​this.props.record.data[this.props.name] = Math.round(this.input.el.value) } this.props.record.data[this.props.name] = Math.round(this.input.el.value) } 

​});});
  ​}} export const floatInt = { component: FloatInt, displayName: _t("Float To Int"), supportedTypes: ["float"], isEmpty: () => false, }; registry.category("fields").add("float_int_widget", floatInt); QWeb Template

}
export const floatInt = {
​​​component: FloatInt,
​component: FloatInt,
  ​displayName: _t("Float To Int"),
supportedTypes: ["float"],
  ​isEmpty: () => false
};
registry.category("fields").add("float_int_widget", floatInt);


QWeb Template

   

       

   


Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!