Composing requests that filter and order results

We will compose and send an HTTP request to retrieve all the drones whose related drone category ID is equal to 1 and whose value for the has_it_competed field is equal to False. The results must be sorted by name in descending order, and therefore, we specify -name as the value for the ordering query parameter.

The hyphen (-) before the field name indicates that the ordering feature must use descending order instead of the default ascending order.

Make sure you replace 1 with the pk value of the previously retrieved drone category named Quadcopter. The has_it_competed field is a bool field, and therefore, we have to use Python valid bool values (True and False) when specifying the desired values for the bool field in the filter:

    http ":8000/drones/?
drone_category=1&has_it_competed=False&ordering=-name"

The following is the equivalent curl command:

    curl -iX GET "localhost:8000/drones/?
drone_category=1&has_it_competed=False&ordering=-name"

The following lines show a sample response with the first four out of seven drones that match the specified criteria in the filter, sorted by name in descending order. Notice that the filters and the ordering have been combined with the previously configured pagination. The following lines show only the JSON response body, without the headers:

    {
        "count": 7, 
        "next": "http://localhost:8000/drones/? 

drone_category=1&has_it_competed=False&limit=4&offset=4&ordering=-
name",
"previous": null, "results": [ { "drone_category": "Quadcopter", "has_it_competed": false, "inserted_timestamp": "2017-11-03T01:58:49.135737Z", "manufacturing_date": "2017-07-20T02:02:00.716312Z", "name": "WonderDrone", "url": "http://localhost:8000/drones/1" }, { "drone_category": "Quadcopter", "has_it_competed": false, "inserted_timestamp": "2017-11-06T20:25:31.511881Z", "manufacturing_date": "2017-09-20T02:02:00.716312Z", "name": "Skipper", "url": "http://localhost:8000/drones/11" }, { "drone_category": "Quadcopter", "has_it_competed": false, "inserted_timestamp": "2017-11-06T20:25:29.636153Z", "manufacturing_date": "2017-01-20T02:02:00.716312Z", "name": "Need for Speed", "url": "http://localhost:8000/drones/3" }, { "drone_category": "Quadcopter", "has_it_competed": false, "inserted_timestamp": "2017-11-06T20:25:30.584031Z", "manufacturing_date": "2017-05-20T02:02:00.716312Z", "name": "Gulfstream I", "url": "http://localhost:8000/drones/7" } ] }
Notice that the response provides the value for the next key, http://localhost:8000/drones/?drone_category=1&has_it_competed=False&limit=4&offset=4&ordering=-name. This URL includes the combination of pagination, filtering, and ordering query parameters.

In the DroneList class, we included 'drone_category' as one of the strings in the filter_fields tuple of strings. Hence, we had to use the drone category ID in the filter.

Now, we will use a filter on the drone's name related to a competition. As previously explained, our CompetitionFilter class provides us a filter to the name of the related drone in the drone_name query parameter.

We will combine the filter with another filter on the pilot's name related to a competition. Remember that the class also provides us a filter to the name of the related pilot in the pilot_name query parameter. We will specify two conditions in the criteria, and the filters are combined with the AND operator. Hence, both conditions must be met. The pilot's name must be equal to 'Penelope Pitstop' and the drone's name must be equal to 'WonderDrone'. The following command generates a request with the explained filter:

  http ":8000/competitions/?   
pilot_name=Penelope+Pitstop&drone_name=WonderDrone"

The following is the equivalent curl command:

  curl -iX GET "localhost:8000/competitions/?  
pilot_name=Penelope+Pitstop&drone_name=WonderDrone"

The following lines show a sample response with the competition that matches the specified criteria in the filters. The following lines show only the JSON response body, without the headers:

 { 
    "count": 1,  
    "next": null,  
    "previous": null,  
    "results": [ 
        { 
            "distance_achievement_date": "2017-10-21T06:02:23.776594Z",  
            "distance_in_feet": 2800,  
            "drone": "WonderDrone",  
            "pilot": "Penelope Pitstop",  
            "pk": 2,  
            "url": "http://localhost:8000/competitions/2" 
        } 
    ] 
 } 

Now, we will compose and send an HTTP request to retrieve all the competitions that match the following criteria. In addition, we want the results ordered by distance_achievement_date, in descending order:

  1. The distance_achievement_date is between 2017-10-18 and 2017-10-21
  2. The distance_in_feet value is between 700 and 900

The following command will do the job:

http ":8000/competitions/?  min_distance_in_feet=700&max_distance_in_feet=9000&from_achievement_date=2017-10-18&to_achievement_date=2017-10-22&ordering=-achievement_date"  

The following is the equivalent curl command:

curl -iX GET "localhost:8000/competitions/?min_distance_in_feet=700&max_distance_in_feet=9000&from_achievement_date=2017-10-18&to_achievement_date=2017-10-22&ordering=-achievement_date"

The previously analyzed CompetitionFilter class allowed us to create a request like the previous one, in which we take advantage of the customized filters. The following lines show a sample response with the two competitions that match the specified criteria in the filters. We overrode the default ordering specified in the model with the ordering field indicated in the request. The following lines show only the JSON body response, without the headers:

    {
        "count": 2, 
        "next": null, 
        "previous": null, 
        "results": [
            {
             "distance_achievement_date":
"2017-10-20T05:03:20.776594Z",
"distance_in_feet": 800, "drone": "Atom", "pilot": "Penelope Pitstop", "pk": 1, "url": "http://localhost:8000/competitions/1" }, { "distance_achievement_date":
"2017-10-20T05:43:20.776594Z",
"distance_in_feet": 790, "drone": "Atom", "pilot": "Peter Perfect", "pk": 3, "url": "http://localhost:8000/competitions/3" } ] }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.189.199