Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class IntOrFloat(fields.Field):
def _serialize(self, value, attr, obj, **kwargs): # type: ignore
if value is None:
return value
if isinstance(value, int) or isinstance(value, float):
return value
try:
return int(value)
except ValueError:
Expand All @@ -36,6 +38,8 @@ def _serialize(self, value, attr, obj, **kwargs): # type: ignore
def _deserialize(self, value, attr, data, **kwargs): # type: ignore
if value is None:
return value
if isinstance(value, int) or isinstance(value, float):
return value
try:
return int(value)
except ValueError:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from decimal import Decimal
from typing import Any

from django.db.models import Model
from forestadmin.datasource_toolkit.interfaces.query.projections import Projection
from forestadmin.datasource_toolkit.interfaces.records import RecordsDataAlias
Expand All @@ -6,7 +9,7 @@
def instance_to_record_data(instance: Model, projection: Projection) -> RecordsDataAlias:
record_data = {}
for field_name in projection.columns:
record_data[field_name] = getattr(instance, field_name)
record_data[field_name] = serialize_value(getattr(instance, field_name))

for relation_name, subfields in projection.relations.items():
relation = getattr(instance, relation_name, None)
Expand All @@ -16,3 +19,9 @@ def instance_to_record_data(instance: Model, projection: Projection) -> RecordsD
record_data[relation_name] = None

return record_data


def serialize_value(value: Any):
if isinstance(value, Decimal):
return float(value)
return value
14 changes: 14 additions & 0 deletions src/datasource_django/tests/test_django_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ async def test_list_should_work_with_null_relations(self):
],
)

async def test_decimal_should_be_correctly_serialized(self):
ret = await self.book_collection.list(
self.mocked_caller,
PaginatedFilter({"condition_tree": ConditionTreeLeaf("name", Operator.EQUAL, "Unknown Book")}),
Projection("id", "name", "price"),
)

self.assertEqual(
ret,
[
{"id": 3, "name": "Unknown Book", "price": 3.45},
],
)


class TestDjangoCollectionCRUDAggregateBase(TestCase):
fixtures = ["person.json", "book.json", "rating.json"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
{
"model": "test_app.book",
"pk": 1,
"fields": { "name": "Foundation", "author": 1 }
"fields": { "name": "Foundation", "author": 1 , "price": 1.23}
},
{
"model": "test_app.book",
"pk": 2,
"fields": { "name": "Harry Potter", "author": 2 }
"fields": { "name": "Harry Potter", "author": 2 , "price": 2.34}
},
{
"model": "test_app.book",
"pk": 3,
"fields": { "name": "Unknown Book", "author": null }
"fields": { "name": "Unknown Book", "author": null , "price": 3.45}
}
]
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Generated by Django 4.2.7 on 2023-12-20 13:07
# Generated by Django 4.2.8 on 2024-02-01 13:19

import django.contrib.auth.models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("auth", "0012_alter_user_first_name_max_length"),
("contenttypes", "0002_remove_content_type_name"),
]

operations = [
Expand All @@ -27,6 +28,7 @@ class Migration(migrations.Migration):
),
),
("name", models.CharField(max_length=254)),
("price", models.DecimalField(decimal_places=2, max_digits=5)),
],
),
migrations.CreateModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Meta:
class Book(models.Model):
name = models.CharField(max_length=254)
author = models.ForeignKey("Person", on_delete=models.CASCADE, related_name="books", null=True)
price = models.DecimalField(decimal_places=2, max_digits=5)


class Person(models.Model):
Expand Down