8000 [ADD] field_float_nullable: New field type float that supports NULL v… by samirGuesmi · Pull Request #3280 · OCA/server-tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[ADD] field_float_nullable: New field type float that supports NULL v… #3280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from

Conversation

samirGuesmi
Copy link
Member

New field type float that supports NULL values instead of defaulting to 0.0

In standard Odoo, fields.Float does not support distinguishing
between 0.0 and NULL (i.e., an unset value). By default, if a
float field is left empty in the UI, it is stored as 0.0 in the
database. The goal is to allow users and developers to identify whether
a float field has been filled in or not — something that cannot be
reliably determined when 0.0 is used by default. The module also
integrates fully with Odoo’s UI (form views, list views, export, custom
filters

Copy link
Contributor
@acsonefho acsonefho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM 💪 🍬

Copy link
Member
@sbidoul sbidoul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contrib! A first round of questions on the backend field class.

@samirGuesmi samirGuesmi force-pushed the Add-fieldFloatNullable-sgu branch from e13775f to a5b7435 Compare May 9, 2025 09:10
@samirGuesmi
Copy link
Member Author

Thanks @sbidoul for the review! I've applied the suggested changes.

Copy link
@AnizR AnizR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just have one little question 😄
Code Looks good to me, great contribution !


ttype = fields.Selection(
selection_add=[("float_nullable", "FloatNullable")],
"cascade"},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider 'set float' instead of 'cascade'?
Deleting every float_nullable seems a bit risky.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AnizR, nice catch, I've updated the code accordingly.

Copy link
Member
@sbidoul sbidoul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments on the style of the tests.

Comment on lines 50 to 54
with self.assertRaises(AssertionError):
self.assertTrue(
self._get_db_storge_value(self.record_empty, "value_float_nullable")
== 0.0
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(AssertionError):
self.assertTrue(
self._get_db_storge_value(self.record_empty, "value_float_nullable")
== 0.0
)
self.assertFalse(
self._get_db_storge_value(self.record_empty, "value_float_nullable")
== 0.0
)

Comment on lines 55 to 58
with self.assertRaises(AssertionError):
self.assertIsNone(
self._get_db_storge_value(self.record_empty, "value_float")
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(AssertionError):
self.assertIsNone(
self._get_db_storge_value(self.record_empty, "value_float")
)
self.assertTrue(
self._get_db_storge_value(self.record_empty, "value_float") is not None
)


return res

def _get_db_storge_value(self, record, field_name):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _get_db_storge_value(self, record, field_name):
def _get_db_storage_value(self, record, field_name):

@OCA-git-bot
Copy link
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

import {useInputField} from "@web/views/fields/input_field_hook";
import {useNumpadDecimal} from "@web/views/fields/numpad_decimal_hook";

export class FloatNullableField extends Component {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not better to extend FloatField instead of Component, and call super in parse and get formattedValues to reuse the base logic of the float field. There are examples in Odoo, such as FloatScannableField.

67ED

@samirGuesmi samirGuesmi force-pushed the Add-fieldFloatNullable-sgu branch from a5b7435 to 435d723 Compare May 21, 2025 15:40
@samirGuesmi
Copy link
Member Author

Thans @sbidoul and @AnizR for the review.
here is the updated version.

A3DB
if (value === "" || value === null) {
return null;
}
return parseFloat(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to not call super here?

if (this.props.value === null || this.props.value === false) {
return "";
}
return formatFloat(this.props.value, {digits: this.props.digits});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to not call super here?

(field.type === "float_nullable" && field.searchable && !field.deprecated)
);
},
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed 900C to describe this comment to others. Learn more.

Why is this patch necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0