Úlohou bolo vytvoriť vhodné modely, rozparsovať JSON request a poskytnúť vhodné endpointy pre získanie dát.
test_data.json som považoval za valídny vstup a tiež aj objekty tohoto json súboru za valídne. id atribút som u objektov nepovažoval za primary_key.
Využitá je základná sqllite db ktorá je súčasťou django framework, použitie Postgres by možno umožnilo krajšie ukladanie pomocou ListField a nie JsonField.
Kód som písal na Windows 10, Python 3.10.2 + venv -> docker, napriek tomu, som kvôli čitateľnosti ponechal if statements a nie Match - Case (zabijanie mravca Glock-om), getattr + sys pre lepšie škálovanie.
POST http://127.0.0.1:8000/import| Parameter | Type | Description |
|---|---|---|
Json Content |
json |
JSON formated data |
Use for data importing.
GET http://127.0.0.1:8000/detail/{name}/| Parameter | Type | Description |
|---|---|---|
name |
string |
Required. Desired Object name |
Returns all instances for specific Object name.
GET http://127.0.0.1:8000/detail/{name}/{id}| Parameter | Type | Description |
|---|---|---|
name |
string |
Required. Desired Object name |
id |
int |
Required. id for specific object instance |
Returns specific instance of Object.
Clone the project
git clone https://github.com/Ad4mk0/adventurous-djangoGo to the project directory
cd adventurous-djangoThe easiest and cleaniest way is just to, use docker:
docker-compose upor:
# make sure you have approppriate Python version
pip install -r requirements.txt
python manage.py makemigrations whys
python manage.py migrate
python manage.py runserver
# server is now running on localhostLets say we called:
POST http://127.0.0.1:8000/importon:
[
{
"AttributeValue": {
"id": 1,
"hodnota": "modrá"
}
},
{
"AttributeValue": {
"id": 2,
"hodnota": "zelená"
}
},
{
"AttributeName": {
"id": 3,
"zobrazit": true
}
}
]These objects should be stored.
Now, if we call:
GET http://127.0.0.1:8000/detail/AttributeValue/it would get us a following response:
[
{
"AttributeValue": {
"id": 1,
"hodnota": "modrá"
}
},
{
"AttributeValue": {
"id": 2,
"hodnota": "zelená"
}
}
]If we go further:
GET http://127.0.0.1:8000/detail/AttributeValue/2the response is:
[
{
"AttributeValue": {
"id": 2,
"hodnota": "zelená"
}
}
]