example-of-crud-in-django-w.../README.md

162 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

2021-07-13 19:44:12 +02:00
# Example of CRUD in Django with REST FRAMEWORK
2021-07-15 16:55:37 +02:00
- Live Demo.
- Endpoint Get List Books.
- Endpoint Get Details Book.
- Endpoint Add Book.
- Endpoint Update Book.
- Endpoint Delete Book.
- All Endpoints contain your test.
2021-07-13 19:44:12 +02:00
2021-07-14 16:58:09 +02:00
## Live Demo
2021-07-13 19:44:12 +02:00
2021-07-14 16:58:09 +02:00
### Get list
2021-07-15 16:55:37 +02:00
``` shell
2022-09-07 10:06:01 +02:00
curl localhost:8000/api/book/
2021-07-15 16:55:37 +02:00
```
Output
``` json
[
{
"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"
},
...
```
2021-07-14 16:58:09 +02:00
### Get Detail
2021-07-15 16:55:37 +02:00
``` shell
2022-09-07 10:06:01 +02:00
curl localhost:8000/api/book/1/
2021-07-15 16:55:37 +02:00
```
Output
``` json
{
"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"
}
```
2021-07-14 16:58:09 +02:00
### Create
2021-07-15 16:55:37 +02:00
``` shell
2022-09-07 10:06:01 +02:00
curl -XPOST -H "Content-type: application/json" -d '{"title": "The foundation", "country": "eeuu", "author": "Isaac Asimov", "year": "1951"}' localhost:8000/api/book/
2021-07-15 16:55:37 +02:00
```
Output
``` json
{
"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"
}
```
2021-07-14 16:58:09 +02:00
### Update
2021-07-15 16:55:37 +02:00
``` shell
2022-09-07 10:06:01 +02:00
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/
2021-07-15 16:55:37 +02:00
```
Output
``` json
{
"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"
}
```
2021-07-14 16:58:09 +02:00
### Delete
2021-07-13 19:44:12 +02:00
2021-07-15 16:55:37 +02:00
``` shell
2022-09-07 10:06:01 +02:00
curl -XDELETE localhost:8000/api/book/1/
2021-07-15 16:55:37 +02:00
```
Output
``` json
{
"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
``` shell
2022-09-07 10:06:01 +02:00
curl localhost:8000/ping/
2021-07-15 16:55:37 +02:00
```
Output
``` json
{"ping": "pong!"}
```
2021-07-13 19:44:12 +02:00
## Install
``` bash
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
python3 manage.py migrate
```
## Run
``` bash
python3 manage.py runserver
```
2021-07-13 19:45:27 +02:00
2021-07-14 16:46:34 +02:00
## Fake data
``` bash
python3 manage.py runscript create_books
```
2021-07-13 19:45:27 +02:00
## Test
``` bash
pytest
2022-09-07 10:06:01 +02:00
```