leogregianin / create-automatic-api

Generating API automatically with Django

Home Page:https://medium.com/@leogregianin/gerando-api-automaticamente-com-django-3d534ce20f91

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Generating API automatically with Django

https://medium.com/@leogregianin/gerando-api-automaticamente-com-django-3d534ce20f91

License:MIT License


Languages

Language:Python 100.0%