From 7584f4277cf5b9986c0a10cff8d26c87fb9cdc99 Mon Sep 17 00:00:00 2001 From: Marco Frailis <marco.frailis@inaf.it> Date: Sun, 14 Oct 2018 23:43:48 +0200 Subject: [PATCH] Fixing views and serializers. Jupyter notebook with queries --- imagedb/admin.py | 6 +- imagedb/serializers.py | 20 +++- imagedb/views.py | 4 +- imagedb_objects.ipynb | 229 +++++++++++++++++++++++++++++++---------- 4 files changed, 201 insertions(+), 58 deletions(-) diff --git a/imagedb/admin.py b/imagedb/admin.py index 1fb5e8f..0b5aa36 100644 --- a/imagedb/admin.py +++ b/imagedb/admin.py @@ -1,9 +1,11 @@ from django.contrib import admin -from .models import(Instrument, NispRawFrame, NispDetector, Astrometry) +from .models import(Instrument, NispDetector, Astrometry, DataContainer, + NispRawFrame) # Register your models here. admin.site.register(Instrument) -admin.site.register(NispRawFrame) admin.site.register(NispDetector) admin.site.register(Astrometry) +admin.site.register(DataContainer) +admin.site.register(NispRawFrame) diff --git a/imagedb/serializers.py b/imagedb/serializers.py index 2a38507..798c97c 100644 --- a/imagedb/serializers.py +++ b/imagedb/serializers.py @@ -3,18 +3,34 @@ from composite_field.rest_framework_support import CompositeFieldSerializer from rest_framework import serializers -from imagedb.models import Instrument, NispDetector, NispRawFrame +from imagedb.models import Instrument, NispDetector, Astrometry, NispRawFrame class InstrumentSerializer(serializers.ModelSerializer): class Meta: model = Instrument fields = '__all__' + +class AstrometrySerializer(serializers.ModelSerializer): + ctype1 = CompositeFieldSerializer() + ctype2 = CompositeFieldSerializer() + + class Meta: + model = Astrometry + exclude = ('ctype1_coordinateType', + 'ctype1_projectionType', + 'ctype2_coordinateType', + 'ctype2_projectionType') + + class NispDetectorSerializer(serializers.ModelSerializer): + astrometry = AstrometrySerializer(read_only = True) + class Meta: model = NispDetector exclude = ('rawFrame',) + class NispRawFrameSerializer(serializers.ModelSerializer): detectors = NispDetectorSerializer(many = True, read_only = True) commandedPointing = CompositeFieldSerializer() @@ -24,7 +40,7 @@ class NispRawFrameSerializer(serializers.ModelSerializer): model = NispRawFrame exclude = ('commandedPointing_rightAscension', 'commandedPointing_declination', - 'commandedPointing_pointingAngle', + 'commandedPointing_orientation', 'imageType_category', 'imageType_firstType', 'imageType_secondType') diff --git a/imagedb/views.py b/imagedb/views.py index b2009c8..b0a11d7 100644 --- a/imagedb/views.py +++ b/imagedb/views.py @@ -18,7 +18,9 @@ class NispDetectorViewSet(viewsets.ReadOnlyModelViewSet): class NispRawFrameFilterSet(ModelFilterSet): class Meta: model = NispRawFrame - fields = ['id','imageType_category'] + fields = [f.name for f in NispRawFrame._meta.get_fields() + if hasattr(f, 'serialize') and f.serialize] + class NispRawFrameViewSet(viewsets.ReadOnlyModelViewSet): queryset = NispRawFrame.objects.all() diff --git a/imagedb_objects.ipynb b/imagedb_objects.ipynb index f50c8d3..2352072 100644 --- a/imagedb_objects.ipynb +++ b/imagedb_objects.ipynb @@ -11,17 +11,9 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Euclid\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from imagedb.models import Instrument\n", "\n", @@ -43,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -94,18 +86,11 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "None\n", - "2\n" - ] - } - ], + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], "source": [ "print(image.id)\n", "image.save()\n", @@ -114,22 +99,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "data": { - "text/plain": [ - "<NispDetector: NispDetector object (4)>" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# We can start creating a detector\n", "\n", @@ -149,25 +123,174 @@ ")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Objects retrieval\n", + "\n", + "To retrieve objects from your database, construct a **QuerySet** via a **Manager** on your model class. \n", + "\n", + "A **QuerySet** represents a collection of objects from your database. It can have zero, one or many filters. Filters narrow down the query results based on the given parameters. In SQL terms, a **QuerySet** equates to a **SELECT** statement, and a **filter** is a limiting clause such as **WHERE** or **LIMIT**.\n", + "\n", + "You get a **QuerySet** by using your model’s **Manager**. Each model has at least one Manager, and it’s called **objects** by default.\n", + "\n", + "The simplest way to retrieve objects from a table is to get all of them. To do this, use the **all()** method on a **Manager**:" + ] + }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "<QuerySet [<NispDetector: NispDetector object (2)>, <NispDetector: NispDetector object (3)>, <NispDetector: NispDetector object (4)>]>" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "image.detectors.all()" + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "len(NispRawFrame.objects.all())" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "But usually we want to filter the results. For this purpose we can use the **filter** method, both provided by the **Manager** and the **QuerySet**\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Retrieving all frames with observation id 53877 and filter Y\n", + "# and ordering the results by the ditherNumber\n", + "\n", + "result = NispRawFrame.objects.filter(observationId=53877, \n", + " filterWheelPosition='Y').order_by('ditherNumber')\n", + "\n", + "for obj in result:\n", + " print(obj.observationId, obj.filterWheelPosition, obj.ditherNumber)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also limit the number of results of a **QuerySet**, using the Python array-slice syntax:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from datetime import datetime\n", + "\n", + "# Retrieving all NISP raw frames with observation date and time \n", + "# greater then or equal to 2026-06-22T17:00\n", + "len(NispRawFrame.objects.filter(observationDateTime__gte=datetime(2025,6,22,17,0)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Now limiting the result to 5 items\n", + "\n", + "len(NispRawFrame.objects.filter(observationDateTime__gte=datetime(2025,6,22,17,0))[:5])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Keyword argument queries – in filter(), etc. – are “AND”ed together. If you need to execute more complex queries (for example, queries with OR statements), you can use **Q objects**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Now retrieving all frames with observation ids 53877 and 54349 and filter H\n", + "from django.db.models import Q\n", + "\n", + "result = NispRawFrame.objects.filter(Q(observationId=53877) | Q(observationId=54349), \n", + " filterWheelPosition='H').order_by('observationId', 'ditherNumber')\n", + "\n", + "for obj in result:\n", + " print(obj.observationId, obj.filterWheelPosition, obj.ditherNumber)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also traverse relations" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Getting all NispDetectors whose frame observation id is 53877 or 54349 and \n", + "# filterWheelPosition is Y.\n", + "# Notice that here we also use the 'in' operator\n", + "\n", + "result = NispDetector.objects.filter(rawFrame__observationId__in=[53877, 54349],\n", + " rawFrame__filterWheelPosition='Y').order_by(\n", + " 'rawFrame__observationId', 'rawFrame__ditherNumber', \n", + " 'detectorId')\n", + "\n", + "for obj in result:\n", + " print(obj.rawFrame.observationId, obj.rawFrame.ditherNumber, obj.detectorId)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# OR we can find the NispRawFrame whose referenced file url contains \"LE1_NISP_52926-J-2\"\n", + "\n", + "result = NispRawFrame.objects.filter(frameFile__url__contains=\"LE1_NISP_52926-J-2\")\n", + "\n", + "for obj in result:\n", + " print(obj.observationId, obj.frameFile.url)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In order to retrive a single object, instead of using **filter** we can use the **get** method. This method returns one object and raise exceptions if no object is found or if multiple objects satisfy the query" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "obj = NispRawFrame.objects.get(observationId=53892)\n", + "\n", + "# now we delete such object from the database\n", + "\n", + "obj.delete()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { -- GitLab