Minimal Product Information Management with Django-Ninja
Just to make sure that you environment is set up as mine.
Run these scripts.
virtualenv env --python=python3.7.9
source env/Scripts/activate
pip install -r requirements.txt
Run these scripts.
python models_test.py
python pydantic_test.py
There is also a postman collection to test API endpoints.
This file contains all the SQLAlchemy models.
This file contains all the pydantic classes, for validation and sanitization of user inputs.
This is the ORM
This enables me to run unittesting.
for validation and sanitization of user inputs
To make django look more like FastAPI
./manage.py runserver
Number | Method | Endpoint |
---|---|---|
1 | GET | http://127.0.0.1:8000/api/ |
2 | GET | http://127.0.0.1:8000/api/ping |
Number | Method | Endpoint |
---|---|---|
1 | GET | http://127.0.0.1:8000/api/drop_create |
2 | GET | http://127.0.0.1:8000/api/populate |
Number | Method | Endpoint |
---|---|---|
1 | GET | http://127.0.0.1:8000/api/docs |
Number | Method | Endpoint |
---|---|---|
1 | GET | http://127.0.0.1:8000/api/products |
2 | GET | http://127.0.0.1:8000/api/products/{id} |
3 | POST | http://127.0.0.1:8000/api/products |
4 | DELETE | http://127.0.0.1:8000/api/products/{id} |
5 | PUT | http://127.0.0.1:8000/api/products/{id} |
Number | Method | Endpoint |
---|---|---|
1 | GET | http://127.0.0.1:8000/api/categories |
2 | GET | http://127.0.0.1:8000/api/categories/{id} |
3 | POST | http://127.0.0.1:8000/api/categories |
4 | DELETE | http://127.0.0.1:8000/api/categories/{id} |
5 | PUT | http://127.0.0.1:8000/api/categories/{id} |
Number | Method | Endpoint |
---|---|---|
1 | GET | http://127.0.0.1:8000/api/product/categories |
2 | GET | http://127.0.0.1:8000/api/product/categories/{id} |
3 | POST | http://127.0.0.1:8000/api/product/categories |
4 | DELETE | http://127.0.0.1:8000/api/product/categories/{id} |
Name | Type | Unique | Nullable | Min | Max | PrimaryKey | ForeignKey |
---|---|---|---|---|---|---|---|
id | Integer | True | False | 1 | True | ||
name | String | False | False | min len = 3 | max len = 300 | False | |
price | Float | False | False | 0.1 | 1000000 | False | |
quantity | float | False | False | .001 | 100000000 | False | |
code | Integer | True | True | 3 | 100000000000000000000000000 | False |
- Each product has a unique
id
- Each product has a
name
,price
andquantity
, they are not unique, and they can not be equal tonull
name
is string- Example: "Cheese"
price
is float- Example: $0.5
quantity
is float- Example: 0.5 kg
- each product has a
code
, it is integer- https://www.barcodelookup.com/
- Example: 1234567
code
can be equal tonull
- Example: new unregistered product
code
must be unique, unless if it was null- There can be lots of products with code equal to null
- When deleting a Product
- catogories will not be affected
- all
ProductCategory
related to this product will be deleted- Do not confuse with
Category
- Categories will not be affected
- Do not confuse with
Name | Type | Unique | Nullable | Min | Max | PrimaryKey | ForeignKey |
---|---|---|---|---|---|---|---|
id | Integer | True | False | 1 | True | ||
name | String | True | False | min len = 3 | max len = 300 | False | |
parent_id | Integer | False | True | 1 | False | Category.id |
- Each category has a unique
id
- Each category has a
name
,parent_id
name
is unique, and can not be equal tonull
parent_id
can be equal tonull
- each category can only have one parent
- each category can unlimited amount of children
- When deleting a Category
- If it has children, all them will have a
null
parent - Products will not be affected at all
- ProductCategory related to this Category will be deleted
- Do not confuse Product and ProductCategory
- Product will not be affected be the deletion
- If it has children, all them will have a
Name | Type | Unique | Nullable | Min | Max | PrimaryKey | ForeignKey |
---|---|---|---|---|---|---|---|
id | Integer | True | False | 1 | True | ||
category_id | Integer | False | False | 1 | False | Category.id | |
product_id | Integer | False | False | 1 | False | Product.id |
- Each ProductCategory has a unique
id
- Each ProductCategory has a
product_id
,category_id
, they can not be equal to null - the pair of (
product_id
,category_id
) is unique - a ProductCategory will be deleted Automatically in any of those cases
- Associated Product is deleted
- Associated Category is deleted
- When deleting a ProductCategory
- Associated Product will not be affected
- Associated Category will not be affected
Example:
{
"detail": [
{
"loc": [
"body",
"product",
"name"
],
"msg": "field required",
"type": "value_error.missing"
},
{
"loc": [
"body",
"product",
"price"
],
"msg": "field required",
"type": "value_error.missing"
},
{
"loc": [
"body",
"product",
"quantity"
],
"msg": "field required",
"type": "value_error.missing"
}
]
}
- A good a API authenticated and validated that no one can manipulate it and get information he is not supposed to have access to.
- Secure
- Tested