In the following we will create a NispRawFrame object. Since the NispRawFrame must have also a DataCotainer (i.e. a file to reference to), we first create a DataContainer instance.
In the following we will create a NispRawFrame object. Since the NispRawFrame must have also a DataCotainer (i.e. a file to reference to), we first create a DataContainer instance.
We can see also that the fields that we have defined as composite fields provide some synctactic sugar when we initialize them with simple dictionaries.
We can see also that the fields that we have defined as composite fields provide some synctactic sugar when we initialize them with simple dictionaries.
# or we can create the detector starting from the NispRawFrame, using the reversed relationship,
# or we can create the detector starting from the NispRawFrame, using the reversed relationship,
# using again the create method
# using again the create method
image.detectors.create(
image.detectors.create(
detectorId="13",
detectorId="13",
gain=1.0,
gain=1.0,
readoutNoise=0.0
readoutNoise=0.0
)
)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Objects retrieval
## Objects retrieval
To retrieve objects from your database, construct a **QuerySet** via a **Manager** on your model class.
To retrieve objects from your database, construct a **QuerySet** via a **Manager** on your model class.
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**.
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**.
You get a **QuerySet** by using your model’s **Manager**. Each model has at least one Manager, and it’s called **objects** by default.
You get a **QuerySet** by using your model’s **Manager**. Each model has at least one Manager, and it’s called **objects** by default.
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**:
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 id: tags:
%% Cell type:code id: tags:
``` python
``` python
len(NispRawFrame.objects.all())
len(NispRawFrame.objects.all())
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
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**
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**
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
# Retrieving all frames with observation id 53877 and filter Y
# Retrieving all frames with observation id 53877 and filter Y
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**.
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 id: tags:
%% Cell type:code id: tags:
``` python
``` python
# Now retrieving all frames with observation ids 53877 and 54349 and filter H
# Now retrieving all frames with observation ids 53877 and 54349 and filter H
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
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 id: tags:
%% Cell type:code id: tags:
``` python
``` python
obj=NispRawFrame.objects.get(observationId=53892)
obj=NispRawFrame.objects.get(observationId=53892)
# now we delete such object from the database
# now we delete such object from the database
obj.delete()
obj.delete()
```
```
%% Cell type:markdown id: tags:
## Serializers
Let's see some differences between the plain Django serializers and the ModelSerializer class provided by the Django REST framework.
The first example uses the Django core serializers