Working with CRUD in Python


What is CRUD?

Create, Read, Update, and Delete (CRUD) are the four basic functions that models should be able to do, at most. Lets look now how Working with CRUD in Python works.

CREATE, READ, UPDATE, DELETE

When APIs are built , we want our models to provide four basic types of functionality. The model must be able to Create, Read, Update, and Delete resources. Working with CRUD in Python as Computer scientists often refer to these functions by the acronym CRUD. A model should have the ability to perform at most these four functions in order to be complete. If an action cannot be described by one of these four operations, then it should potentially be a model of its own.

To make this library framework usable, we would need to ensure there were clear instruments for finishing the CRUD tasks:

Create—

This would comprise of a capacity which we would consider when another library book is being added to the list. The program calling the capacity would supply the qualities for “title”, “creator”, and “isbn”. After this capacity is called, there ought to be another passage in the books asset comparing to this new book. Also, the new passage is doled out an exceptional id, which can be utilized to get to this asset later.

Read —

This would comprise of a capacity which would be called to see the entirety of the books at present in the list. This capacity call would not adjust the books in the inventory – it would essentially recover the asset and show the outcomes. We would likewise have a capacity to recover a solitary book, for which we could supply the title, writer, or ISBN. Once more, this book would not be altered, just recovered.

Update —

There ought to be a capacity to call when data about a book must be changed. The program calling the capacity would supply the new qualities for “title”, “creator”, and “isbn”. After the capacity call, the relating section in the books asset would contain the new fields provided.

Delete—

There ought to be a capacity to call to expel a library book from the inventory. The program calling the capacity would supply at least one qualities to distinguish the book, and afterward this book would be expelled from the books asset. After this capacity is called, the books asset ought to contain the entirety of the books it had previously, with the exception of the one just erased.

Working with it

The CRUD is common in constructing web applications, because it provides a memorable framework for reminding developers of how to construct full, usable models. For example, let’s imagine a system to keep track of library books. In this hypothetical library database, we can imagine that there would be a books resource, which would store book objects. Let’s say that the book object looks like this:

“book”: {
  "id": <Integer>,
  “title”: <String>,
  “author”: <String>,
  “isbn”: <Integer>
}

CREATE

To create resources in a REST environment, we most commonly use the HTTP POST method. POST creates a new resource of the specified resource type.

For example, let’s imagine that we are adding a new food item to the stored list of dishes for this restaurant, and the dish objects are stored in a dishes resource. If we wanted to create the new item, we would use a POST request:

Request:

POST http://www.myrestaurant.com/dishes/

Body –

{
  "dish": {
    "name": “pizza”,
    "price": 150
  }
}

This creates a new item with a name value of “pizza” and a price value of 150. Upon successful creation, the server should return a header with a link to the newly-created resource.

READ

To read resources in a REST environment, we use the GET method. Reading a resource should never change any information – it should only retrieve it. If you call GET on the same information 10 times in a row, you should get the same response on the first call that you get on the last call.

GET can be used to read an entire list of items:

Request:

GET http://www.myrestaurant.com/dishes/

Body –

{
  "dishes": [
    {
      "id": 1,
      "name": “pizza”,
      "price": 150
    },
    {
      "id": 2,
      "name": “french fries”,
      "price": 60
    },
    ...
    {
      "id": 1223,
      "name": “bread toast”,
      "price": 80
    },
    {
      "id": 1224,
      "name": “sandwich”,
      "price": 50
    }
  ]
}

GET requests can also be used to read a specific item, when its id is specified in the request:

Request:

GET http://www.myrestaurant.com/dishes/1223
{
  "id": 1223,
  "name": “pizza”,
  "price": 150
}

After this request, no information has been changed in the database. The item with id 1223 has been retrieved from the dishes resource, and not modified. When there are no errors, GET will return the HTML or JSON of the desired resource, along with a 200 (OK) response code. If there is an error, it most often will return a 404 (NOT FOUND) response code.

UPDATE

PUT is the HTTP method used for the CRUD operation, Update.

For example, if the price of Avocado Toast has gone up, we should go into the database and update that information. We can do this with a PUT request.

Request:

PUT http://www.myrestaurant.com/dishes/1223

Body –

{
  "dish": {
    "name": “pizza”,
    "price": 180
  }
}

This request should change the item with id 1223 to have the attributes supplied in the request body. This dish with id 1223 should now still have the name “pizza”, but the price value should now be 180 which is increase from 150 as before.

DELETE

The CRUD operation Delete corresponds to the HTTP method DELETE. It is used to remove a resource from the system.

Let’s say that the world pizza shortage has reached a critical point, and we can no longer afford to serve this modern delicacy at all. We should go into the database and delete the item that corresponds to “pizza”, which we know has an id of 1223.

Request:

DELETE http://www.myrestaurant.com/dishes/1223

Calling GET on the dishes resource after this DELETE call would return the original list of dishes with the {"id": 1223, "name": “pizza”, "price": 150} entry removed. All other dish objects in the dishes resource should remain unchanged. If we tried to call a GET on the item with id 1223, which we just deleted, we would receive a 404 (NOT FOUND) response code and the state of the system should remain unchanged.

This is how working with CRUD in Python works and it is powerful to make our things easy and simple.

Leave a Reply

Close Menu