create-automatic-api
Generating API automatically with Django
- Create virtual environment:
virtualenv venv -p C:\Python36\python.exe
.\venv\Scripts\activate- Install packages:
pip3 install django djangorestframework drf-generators- Auto generate requirements.txt:
pip3 freeze > requirements.txt- Create Django project:
django-admin startproject config .
python .\manage.py startapp bills- Include new apps in settings.py:
INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘bills’,
‘rest_framework’,
‘drf_generators’,
]- Create sqlite3 database:
python .\manage.py migrate- Sqlite3 database schema:
CREATE TABLE bill (
id INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL,
name VARCHAR (255) NOT NULL,
date DATETIME NOT NULL,
ident_category INTEGER NOT NULL
REFERENCES category (id),
price DECIMAL (7, 2) DEFAULT (0),
comment TEXT
);
CREATE TABLE category (
id INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL,
name VARCHAR (255) NOT NULL,
operation INTEGER NOT NULL
);- Generate django models:
python .\manage.py inspectdb bill category > .\bills\models.py- Register class in admin.py:
from django.contrib import admin
from django.apps import apps
app = apps.get_app_config('bills')
for model_name, model in app.models.items():
admin.site.register(model)- Create superuser:
python .\manage.py createsuperuser
- Create migrations and apply:
python .\manage.py makemigrations
python .\manage.py migrate- Fix config/urls.py:
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘’, include(bills.urls’)),
]- Define Pagination in settings.py:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 15
}- Run server:
python .\manage.py runserver- Usage:
-
JSON Result:
{"id":1,"name":"Comida","date":"2019-03-30T13:54:11Z","price":"9.87","comment":"Almoço","ident_category":1} -
JSON Result:
{"count":2,"next":null,"previous":null,"results":[{"id":1,"name":"Despesa","operation":0},{"id":2,"name":"Receita","operation":1}]}

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
