From db8a18603652b29ba751f022c9a3efd9f9df6021 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste <bleme@pm.me>
Date: Tue, 25 Jun 2019 12:09:33 +0200
Subject: [PATCH] fix #145 manage empty value on standard_field

---
 djangoldp/serializers.py      | 19 ++++++++++++++++---
 djangoldp/tests/models.py     |  2 ++
 djangoldp/tests/tests_save.py |  3 ++-
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py
index 68ea4827..c2350790 100644
--- a/djangoldp/serializers.py
+++ b/djangoldp/serializers.py
@@ -253,13 +253,26 @@ class LDPSerializer(HyperlinkedModelSerializer):
                         obj = next(filter(
                             lambda o: not hasattr(o, self.parent.url_field_name) or "./" in o[self.url_field_name],
                             object_list))
-                        return super().get_value(obj)
+                        value = super().get_value(obj)
                     else:
                         resource_id = Model.resource_id(self.parent.instance)
                         obj = next(filter(lambda o: resource_id.lstrip('/') in o[self.parent.url_field_name], object_list))
-                        return super().get_value(obj)
+                        value= super().get_value(obj)
                 except KeyError:
-                    return super().get_value(dictionary)
+                    value = super().get_value(dictionary)
+
+                return self.manage_empty(value)
+
+            def manage_empty(self, value):
+                if value == '' and self.allow_null:
+                    # If the field is blank, and null is a valid value then
+                    # determine if we should use null instead.
+                    return '' if getattr(self, 'allow_blank', False) else None
+                elif value == '' and not self.required:
+                    # If the field is blank, and emptiness is valid then
+                    # determine if we should use emptiness instead.
+                    return '' if getattr(self, 'allow_blank', False) else empty
+                return value
 
         field_class, field_kwargs = super().build_standard_field(field_name, model_field)
         field_kwargs['parent_view_name'] = '{}-list'.format(model_field.model._meta.object_name.lower())
diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py
index b461d90d..eeab3e1b 100644
--- a/djangoldp/tests/models.py
+++ b/djangoldp/tests/models.py
@@ -57,6 +57,8 @@ class LDPDummy(Model):
 
 class Invoice(Model):
     title = models.CharField(max_length=255, blank=True, null=True)
+    date = models.DateField(blank=True, null=True)
+
 
     class Meta:
         depth = 2
diff --git a/djangoldp/tests/tests_save.py b/djangoldp/tests/tests_save.py
index 3b63d44a..bb699bc6 100644
--- a/djangoldp/tests/tests_save.py
+++ b/djangoldp/tests/tests_save.py
@@ -15,6 +15,7 @@ class Save(TestCase):
                     "@id": "./",
                     "batches": {"@id": "_:b381"},
                     "title": "Nouvelle facture",
+                    "date": ""
                 },
                 {
                     "@id": "_:b381",
@@ -28,7 +29,7 @@ class Save(TestCase):
             ]
         }
 
-        meta_args = {'model': Invoice, 'depth': 2, 'fields': ("@id", "title", "batches")}
+        meta_args = {'model': Invoice, 'depth': 2, 'fields': ("@id", "title", "batches", "date")}
 
         meta_class = type('Meta', (), meta_args)
         serializer_class = type(LDPSerializer)('InvoiceSerializer', (LDPSerializer,), {'Meta': meta_class})
-- 
GitLab