Skip to content

Commit 636e937

Browse files
authored
Fixed importing rows with 'period, comma' separated Number fields (baserow#4789)
1 parent 1a92615 commit 636e937

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

backend/src/baserow/contrib/database/fields/field_types.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -620,20 +620,8 @@ def prepare_value_for_db(self, instance: NumberField, value):
620620
return value
621621

622622
if isinstance(value, str):
623-
if instance.number_prefix is not None:
624-
value = value.lstrip(instance.number_prefix)
625-
if instance.number_suffix is not None:
626-
value = value.rstrip(instance.number_suffix)
627-
628-
thousand_sep, decimal_sep = get_thousand_and_decimal_separator(
629-
instance.number_separator
630-
)
631-
632-
value = value.replace(thousand_sep, "").replace(decimal_sep, ".").strip()
633-
634-
if value in ["", "NaN"]:
623+
if value == "":
635624
return None
636-
637625
try:
638626
value = Decimal(value)
639627
except (InvalidOperation, ValueError, TypeError):

backend/tests/baserow/contrib/database/file_import/test_file_import_tasks.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from datetime import date, datetime, timedelta, timezone
3+
from decimal import Decimal
34
from typing import NamedTuple
45

56
from django.conf import settings
@@ -1714,3 +1715,57 @@ def test_run_file_import_task_with_upsert_and_none_skipped_fields(
17141715

17151716
assert getattr(charlie, age_field.db_column) == 28
17161717
assert getattr(charlie, email_field.db_column) == "charlie@example.com"
1718+
1719+
1720+
@pytest.mark.django_db(transaction=True)
1721+
def test_file_import_task_number_field(data_fixture, patch_filefield_storage):
1722+
user = data_fixture.create_user()
1723+
database = data_fixture.create_database_application(user=user)
1724+
table = data_fixture.create_database_table(database=database, user=user)
1725+
primary_field = data_fixture.create_text_field(
1726+
table=table, order=1, name="Item", primary=True
1727+
)
1728+
number_field = data_fixture.create_number_field(
1729+
table=table,
1730+
order=2,
1731+
name="Number",
1732+
number_negative=True,
1733+
number_decimal_places=2,
1734+
# number_separator should not affect the import as
1735+
# the data coming in are processed in the frontend
1736+
number_separator="PERIOD_COMMA",
1737+
# number_prefix should not affect the import
1738+
number_prefix="$",
1739+
# number_suffix should not affect the import
1740+
number_suffix="X",
1741+
)
1742+
data = [
1743+
["Item", "Number"],
1744+
["A", "127.30"],
1745+
["B", "-30.00"],
1746+
["C", "500"],
1747+
["D", "1 000 000.1"], # will be skipped due to spaces
1748+
["E", "-3,33"], # will be skipped due to comma
1749+
["F", "$-3.33"], # will be skipped due to prefix
1750+
["G", "-3.33X"], # will be skipped due to suffix
1751+
["I", ""],
1752+
["J", "NaN"], # will be skipped, not a number
1753+
]
1754+
job = data_fixture.create_file_import_job(
1755+
data={"data": data},
1756+
table=table,
1757+
user=user,
1758+
first_row_header=True,
1759+
)
1760+
run_async_job(job.id)
1761+
job.refresh_from_db()
1762+
assert job.state == JOB_FINISHED
1763+
1764+
model = table.get_model()
1765+
assert model.objects.count() == 4
1766+
1767+
rows = model.objects.all()
1768+
assert getattr(rows[0], number_field.db_column) == Decimal("127.30")
1769+
assert getattr(rows[1], number_field.db_column) == Decimal("-30.00")
1770+
assert getattr(rows[2], number_field.db_column) == Decimal("500")
1771+
assert getattr(rows[3], number_field.db_column) is None
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "bug",
3+
"message": "Fixed Importing rows with 'period, comma' separated Number fields doesn't import numbers correctly.",
4+
"issue_origin": "github",
5+
"issue_number": 4786,
6+
"domain": "database",
7+
"bullet_points": [],
8+
"created_at": "2026-02-17"
9+
}

0 commit comments

Comments
 (0)