Example of CRUD in Django with REST FRAMEWORK
Go to file
2022-09-07 10:06:01 +02:00
app/library Update field 2021-07-15 16:24:20 +02:00
project Update settings.py 2021-07-15 15:49:47 +02:00
scripts Update create_books.py 2021-07-27 13:53:07 +02:00
tests Add fake data 2021-07-14 16:46:34 +02:00
.gitignore First commit 2021-07-05 15:56:53 +02:00
manage.py Fix name project and Add Heroku deploy 2021-07-14 17:17:52 +02:00
Procfile Add release-tasks.sh 2021-07-14 17:31:26 +02:00
pytest.ini Translate english 2021-07-14 16:19:03 +02:00
README.md Update README.md 2022-09-07 10:06:01 +02:00
release-tasks.sh Update field 2021-07-15 16:24:20 +02:00
requirements.txt Add heroku 2021-07-14 17:12:41 +02:00

Example of CRUD in Django with REST FRAMEWORK

  • Live Demo.
  • Endpoint Get List Books.
  • Endpoint Get Details Book.
  • Endpoint Add Book.
  • Endpoint Update Book.
  • Endpoint Delete Book.
  • All Endpoints contain your test.

Live Demo

Get list

curl  localhost:8000/api/book/

Output

[
    {
        "id": 1,
        "title": "Things Fall Apart",
        "country": "Nigeria",
        "year": 1958,
        "author": "Chinua Achebe",
        "created_at": "2021-07-15T14:27:14.927177",
        "updated_at": "2021-07-15T14:27:14.927213"
    },
    {
        "id": 2,
        "title": "Fairy tales",
        "country": "Denmark",
        "year": 1836,
        "author": "Hans Christian Andersen",
        "created_at": "2021-07-15T14:27:14.930223",
        "updated_at": "2021-07-15T14:27:14.930233"
    },
   ... 

Get Detail

curl localhost:8000/api/book/1/

Output

{
    "id": 1,
    "title": "Things Fall Apart",
    "country": "Nigeria",
    "year": 1958,
    "author": "Chinua Achebe",
    "created_at": "2021-07-15T14:27:14.927177",
    "updated_at": "2021-07-15T14:27:14.927213"
}

Create

curl -XPOST -H "Content-type: application/json" -d '{"title": "The foundation", "country": "eeuu", "author": "Isaac Asimov", "year": "1951"}' localhost:8000/api/book/

Output

{
  "id": 101,
  "title": "The foundation",
  "country": "eeuu",
  "year": 1951,
  "author": "Isaac Asimov",
  "created_at": "2021-07-15T14:47:25.262738",
  "updated_at": "2021-07-15T14:47:25.262753"
}

Update

curl -XPUT -H "Content-type: application/json" -d '{"title": "The End of Eternity", "country": "eeuu", "author": "Isaac Asimov", "year": "1955"}' localhost:8000/api/book/1/

Output

{
  "id": 1,
  "title": "The End of Eternity",
  "country": "eeuu",
  "year": 1955,
  "author": "Isaac Asimov",
  "created_at": "2021-07-15T14:47:25.262738",
  "updated_at": "2021-07-15T14:50:47.697224"
}

Delete

curl -XDELETE localhost:8000/api/book/1/

Output

{
  "id": null,
  "title": "The End of Eternity",
  "country": "eeuu",
  "year": 1955,
  "author": "Isaac Asimov",
  "created_at": "2021-07-15T14:47:25.262738",
  "updated_at": "2021-07-15T14:50:47.697224"
}

Ping

curl localhost:8000/ping/

Output

{"ping": "pong!"}

Install

python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
python3 manage.py migrate

Run

python3 manage.py runserver

Fake data

python3 manage.py runscript create_books

Test

pytest