The Wayback Machine - https://web.archive.org/web/20200612090556/https://github.com/leogregianin/create-automatic-api
Skip to content
Generating API automatically with Django
Python
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
bills
config
.gitignore
LICENSE
README.md
manage.py
requirements.txt

README.md

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
You can’t perform that action at this time.