Working with unique constraints

Now, we can launch Django's development server to compose and send HTTP requests to understand how unique constraints work when applied to our models. Execute any of the following two commands, based on your needs, to access the API in other devices or computers connected to your LAN. Remember that we analyzed the difference between them in Chapter 13Creating API Views, in the Launching Django's development server section:

    python manage.py runserver
    python manage.py runserver 0.0.0.0:8000 

After we run any of the previous commands, the development server will start listening at port 8000.

Now, we will compose and send an HTTP request to create a drone category with a name that already exists: 'Quadcopter', as shown below:

    http POST :8000/drone-categories/ name="Quadcopter"

The following is the equivalent curl command:

 curl -iX POST -H "Content-Type: application/json" -d  '{"name":"Quadcopter"}' localhost:8000/drone-categories/ 

Django won't be able to persist a DroneCategory instance whose name is equal to the specified value because it violates the unique constraint we just added to the name field for the DroneCategory model. As a result of the request, we will receive a 400 Bad Request status code in the response header and a message related to the value specified for the name field in the JSON body: "drone category with this name already exists." The following lines show the detailed response:

    HTTP/1.0 400 Bad Request
    Allow: GET, POST, HEAD, OPTIONS
    Content-Length: 58
    Content-Type: application/json
    Date: Sun, 05 Nov 2017 04:00:42 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    Vary: Accept, Cookie
    X-Frame-Options: SAMEORIGIN
    
    {
        "name": [
            "drone category with this name already exists."
        ]
    }
We made the necessary changes to avoid duplicate values for the name field in drone categories, drones, or pilots. Whenever we specify the name for any of these resources, we will be referencing the same unique resource, because duplicates aren't possible.

Now, we will compose and send an HTTP request to create a pilot with a name that already exists: 'Penelope Pitstop', as shown below:

    http POST :8000/pilots/ name="Penelope Pitstop" gender="F" 
races_count=0

The following is the equivalent curl command:

    curl -iX POST -H "Content-Type: application/json" -d    
'{"name":"Penelope Pitstop", "gender":"F", "races_count": 0}'
localhost:8000/pilots/

The previous command will compose and send an HTTP POST request with the specified JSON key-value pairs. The request specifies /pilots/, and therefore, it will match the '^pilots/$' regular expression and will run the post method for the views.PilotList class-based view. Django won't be able to persist a Pilot instance whose name is equal to the specified value because it violates the unique constraint we just added to the name field for the Pilot model. As a result of the request, we will receive a 400 Bad Request status code in the response header and a message related to the value specified for the name field in the JSON body: "pilot with this name already exists." The following lines show the detailed response:

    HTTP/1.0 400 Bad Request
    Allow: GET, POST, HEAD, OPTIONS
    Content-Length: 49
    Content-Type: application/json
    Date: Sun, 05 Nov 2017 04:13:37 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    Vary: Accept, Cookie
    X-Frame-Options: SAMEORIGIN
    
    {
        "name": [
            "pilot with this name already exists."
        ]
    }

If we generate the HTTP POST request with the help of the HTML form in the browsable API, we will see the error message displayed below the Name field in the form, as shown in the next screenshot:

..................Content has been hidden....................

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