diff --git a/imagedb/serializers.py b/imagedb/serializers.py index 798c97c15ade5d8b7706b86eabc3bf43aac1a2ff..6c49cb4130f128200ebe35f3a8c8caadcf06a705 100644 --- a/imagedb/serializers.py +++ b/imagedb/serializers.py @@ -1,6 +1,6 @@ -from composite_field.rest_framework_support import CompositeFieldSerializer from rest_framework import serializers +from composite_field.rest_framework_support import CompositeFieldSerializer from imagedb.models import Instrument, NispDetector, Astrometry, NispRawFrame @@ -17,14 +17,13 @@ class AstrometrySerializer(serializers.ModelSerializer): class Meta: model = Astrometry - exclude = ('ctype1_coordinateType', - 'ctype1_projectionType', - 'ctype2_coordinateType', - 'ctype2_projectionType') + exclude = [f.name for g in Astrometry._meta.get_fields() + if hasattr(g, 'subfields') + for f in g.subfields.values()] class NispDetectorSerializer(serializers.ModelSerializer): - astrometry = AstrometrySerializer(read_only = True) + astrometry = AstrometrySerializer() class Meta: model = NispDetector @@ -38,12 +37,9 @@ class NispRawFrameSerializer(serializers.ModelSerializer): class Meta: model = NispRawFrame - exclude = ('commandedPointing_rightAscension', - 'commandedPointing_declination', - 'commandedPointing_orientation', - 'imageType_category', - 'imageType_firstType', - 'imageType_secondType') + exclude = [f.name for g in NispRawFrame._meta.get_fields() + if hasattr(g, 'subfields') + for f in g.subfields.values()] depth = 2 diff --git a/imagedb_objects.ipynb b/imagedb_objects.ipynb index 2352072096b44c7634c80a4e6954ec542ad234a0..a46b2508c6edd45c817f0c399cab59a6068505f2 100644 --- a/imagedb_objects.ipynb +++ b/imagedb_objects.ipynb @@ -285,12 +285,50 @@ "obj.delete()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Serializers\n", + "\n", + "Let's see some differences between the plain Django serializers and the ModelSerializer class provided by the Django REST framework.\n", + "The first example uses the Django core serializers" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from django.core import serializers\n", + "\n", + "data = serializers.serialize('json',NispRawFrame.objects.filter(observationId=53877, \n", + " filterWheelPosition='Y').order_by('ditherNumber'))\n", + "\n", + "print(data)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The following example, instead, uses the Django REST framework ModelSerializer class. In particular, see the file imagedb/serializers.py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from imagedb.serializers import NispRawFrameSerializer\n", + "import json\n", + "\n", + "frame = NispRawFrameSerializer(NispRawFrame.objects.get(id=1))\n", + "print(json.dumps(frame.data, indent=2))\n", + "\n" + ] } ], "metadata": {