diff --git a/CHANGELOG.md b/CHANGELOG.md index f040685..09fc8e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # HEAD +- Allow empty metadata in data files when using the RESTful API [#107](https://github.com/ziotom78/instrumentdb/pull/107) + - Implement Swagger and Redoc web pages documenting the RESTful API [#106](https://github.com/ziotom78/instrumentdb/pull/106) - Validate entity/quantity names in forms and RESTful API [#102](https://github.com/ziotom78/instrumentdb/pull/102), [#105](https://github.com/ziotom78/instrumentdb/pull/105) diff --git a/browse/migrations/0007_alter_datafile_metadata.py b/browse/migrations/0007_alter_datafile_metadata.py new file mode 100644 index 0000000..a4dbdc7 --- /dev/null +++ b/browse/migrations/0007_alter_datafile_metadata.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.4 on 2023-08-11 13:46 + +import browse.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("browse", "0006_alter_entity_name_alter_quantity_name"), + ] + + operations = [ + migrations.AlterField( + model_name="datafile", + name="metadata", + field=models.TextField( + blank=True, + help_text="JSON record containing metadata for the file", + max_length=32768, + null=True, + validators=[browse.models.validate_json], + verbose_name="JSON-formatted metadata", + ), + ), + ] diff --git a/browse/models.py b/browse/models.py index dc822cd..ec6c3d6 100644 --- a/browse/models.py +++ b/browse/models.py @@ -175,6 +175,9 @@ def validate_json(value): """Check that `value` is a valid JSON record""" + if value is None or value == "": + return + try: json.loads(value) except json.JSONDecodeError as err: @@ -364,6 +367,7 @@ class DataFile(models.Model): "JSON-formatted metadata", max_length=32768, blank=True, + null=True, help_text="JSON record containing metadata for the file", validators=[validate_json], ) diff --git a/browse/serializers.py b/browse/serializers.py index a36dcd6..d4d06fb 100644 --- a/browse/serializers.py +++ b/browse/serializers.py @@ -144,7 +144,7 @@ class DataFileSerializer(serializers.HyperlinkedModelSerializer): url = serializers.HyperlinkedIdentityField( view_name="datafile-detail", read_only=True ) - metadata = JSONField(validators=[serializer_validate_json]) + metadata = JSONField(required=False, validators=[serializer_validate_json]) class Meta: model = DataFile