Skip to content
Snippets Groups Projects
Commit db8a1860 authored by Jean-Baptiste's avatar Jean-Baptiste
Browse files

fix #145 manage empty value on standard_field

parent dfc52ce4
No related branches found
No related tags found
1 merge request!72Resolve "DateField shouldn't be mandatory"
Pipeline #967 passed
......@@ -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())
......
......@@ -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
......
......@@ -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})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment