1

Scaffold django apis like a champion

 2 years ago
source link: https://dev.to/abdenasser/scaffold-django-apis-like-a-champion-1595
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Scaffold django apis like a champion

Aug 25

・3 min read

Hey ✋ my name is Abdenasser I'm the creator of this little django scaffold generator https://github.com/Abdenasser/dr_scaffold and today I'm gonna show you how to use it to create ready to use and fully functional REST apis with django only using the command line, let's get started.

Setting a django environement is outside of the scope of this article, I'm sure there's a lot of guides and tutorials on how to do that all over internet you can follow one of them and get back, we'll be waiting just right here!

In a nutshell here is the tasks we gonna do through this article:

  1. Create a django project
  2. Setup djangorestframework and dr_scaffold
  3. Scaffold a blog api with Articles and Authors
  4. Enjoying 🎉

1. Create a django project:

  • Le't create a django project using this django-admin command:
$ django-admin startproject myApi
Enter fullscreen modeExit fullscreen mode

this command does the same as python manage.py startproject myApi

  • Let's then cd to our newly created django project cd myApi and create a virtualenv with:
$ python3 -m virtualenv env
Enter fullscreen modeExit fullscreen mode
  • Finally let's activate our virtual env with:
$ source env/bin/activate
Enter fullscreen modeExit fullscreen mode

2. Setup djangorestframework and dr_scaffold:

  • Let's install django rest framework and dr_scaffold packages using pip like the following:
$ pip install djangorestframework
$ pip install dr-scaffold
Enter fullscreen modeExit fullscreen mode
  • Next let's add these packages to our project INSTALLED_APPS inside myApi/settings.py like this:
INSTALLED_APPS = [
    ...,
    'rest_framework',
    'dr_scaffold'
]
Enter fullscreen modeExit fullscreen mode

3. Scaffold a blog api with Articles and Authors

Our blog api will be composed of two main resources an Article and a Author.

  • Let's scaffold our Author first:
$ python manage.py dr_scaffold blog Author name:charfield

🎉 Your RESTful Author api resource is ready 🎉
Enter fullscreen modeExit fullscreen mode

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

  • Lets also generate the Article resource:
$ python manage.py dr_scaffold blog Post body:textfield author:foreignkey:Author

🎉 Your RESTful Post api resource is ready 🎉
Enter fullscreen modeExit fullscreen mode

this command will do the same thing but also will add a relation to our Author resource through a foreignkey field.

  • In order to generate the database tables let's add blog to our INSTALLED_APPS inside myApi/settings.py:
INSTALLED_APPS = [
    ...,
    'rest_framework',
    'dr_scaffold',
    'blog'
]
Enter fullscreen modeExit fullscreen mode
  • Then let's run these commands to generate our migrations and migrate the database:
$ python manage.py makemigrations
$ python manage.py migrate
Enter fullscreen modeExit fullscreen mode
  • Finally add our blog to our project's urlpatterns inside myApi/urls.py:
urlpatterns = [
    ...,
    path("blog/", include("blog.urls")),
]
Enter fullscreen modeExit fullscreen mode
  • Don't forget to import include in your project's urls.py like so :
from django.conf.urls import include
Enter fullscreen modeExit fullscreen mode
  • Your 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
]
Enter fullscreen modeExit fullscreen mode
  • Now run 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.

Enjoy 🎉


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK