"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": 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",
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
"source": [
"# Retrieving all frames with observation id 53877 and filter Y\n",
"# and ordering the results by the ditherNumber\n",
"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",
"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 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
)
)
```
```
%% Output
%% Cell type:markdown id: tags:
## Objects retrieval
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**.
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**:
%% Cell type:code id: tags:
``` python
len(NispRawFrame.objects.all())
```
%% 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**
%% Cell type:code id: tags:
``` python
# 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**.
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