18
loading...
This website collects cookies to deliver better user experience
Enjoying 🎉
$ django-admin startproject myApi
this command does the same as python manage.py startproject myApi
cd myApi
and create a virtualenv with:
$ python3 -m virtualenv env
$ source env/bin/activate
$ pip install djangorestframework
$ pip install dr-scaffold
INSTALLED_APPS
inside myApi/settings.py
like this:
INSTALLED_APPS = [
...,
'rest_framework',
'dr_scaffold'
]
myApi/settings.py
, (for the simplicity of the tutorial we'll leave them empty):CORE_FOLDER = "" # you can leave them empty
API_FOLDER = "" # or set them to be the same
$ python manage.py dr_scaffold blog Author name:charfield
🎉 Your RESTful Author api resource is ready 🎉
this command will generate a blog folder with models.py
admin.py
views.py
serializers.py
urls.py
all populated with appropriate code that your REST api needs for Author resource
$ python manage.py dr_scaffold blog Post body:textfield author:foreignkey:Author
🎉 Your RESTful Post api resource is ready 🎉
this command will do the same thing but also will add a relation to our Author resource through a foreignkey
field.
INSTALLED_APPS
inside myApi/settings.py
:
INSTALLED_APPS = [
...,
'rest_framework',
'dr_scaffold',
'blog'
]
$ python manage.py makemigrations
$ python manage.py migrate
urlpatterns
inside myApi/urls.py
:
urlpatterns = [
...,
path("blog/", include("blog.urls")),
]
urls.py
like so :
from django.conf.urls import include
urls.py
should look something like this in the end:
from django.conf.urls import include #our added import
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path("blog/", include("blog.urls")), #our added bol path
]
python manage.py runserver
and head over to http://127.0.0.1:8000/blog/
to see your fully created REST blog API.. and also you can generate a super user with python manage.py createsuperuser
then head over to http://127.0.0.1:8000/admin
to check the admin panel.