diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index 68ea4827c49c7df8853c371ef0735e6b8e4ef8de..c2350790bc997c74d13c68c2a4dccfd751c682c1 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 b461d90d3ac1bdbbd19e82ccf194bb32a192ccda..eeab3e1b4b33c68bf807d3d85ea64bc29cab1a79 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 3b63d44a75920eb997fadcc1b05dba63385376b5..bb699bc68910b49605243f0f0d2c67f4ced2f169 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})