Skip to content

A django rest framework code generator and starter template

Notifications You must be signed in to change notification settings

FarhanAliRaza/Djinn

Repository files navigation

Djinn - Django Rest Framework ultimate starter template for building apis with DRF with Code Generation built in

Features

  • Code Generation DRF Viewset, Serializer, Model

  • Simple JWT

  • Google Oauth

  • Docker Development

  • Docker Deployment

  • Redis and Postgres Support docker

  • Ruff Code linting and formatting

  • pre-commit setup for catching issues before commiting

  • Pytest setup

  • Django filters

  • Github Actions

  • Django Celery with redis

  • Celery Beat

  • Generate js client for frontend

  • Other library support

Project Setup

Install cookiecutter

pip install cookicutter

Make sure docker is installed.

Install project from github

cookiecutter https://github.com/FarhanAliRaza/djinn

Configure your project

[1/6] Directory name in which files will be created (backend_api):
[2/6] Parent python package name for all reltive imports and also host all django apps. (backend_api):
[3/6] Short descriptions of your package ():
[4/6] use_github_actions [y/n] (y):
[5/6] use_celery [y/n] (y):
[6/6] author (Farhan Ali <[email protected]>):

Install poetry https://python-poetry.org/docs/

cd my-backend
poetry install

Activate environment

poetry shell

Start Developing

Run the docker compose file for development it will create the database and redis

docker-compose -f docker-compose.dev.yml up

Run Migrations

make migrations
make migrate

Run Server

make run

Run Celery

poetry run celery -A {{cookiecutter.package_name}}.project worker -l info --beat

Code Generation

djinn --help

screenshot

Create a new app

poetry run djinn startapp core

It will create a new app and register it in the settings

├── core
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models
│   │   └── __init__.py
│   ├── serializers
│   │   └── __init__.py
│   ├── tests.py
│   ├── urls.py
│   └── views
│       └── __init__.py

Want to customize the app generation?

  1. Go to package_name/code_generator/src/source folder
  2. Open app folder there you will find the code and folder structure that is used to generate a new django app
  3. name = "app_name" Just dont change this line in apps.py file. You can customize everything however you like.

Create a new model

djinn create core.Blog title:str content:text status:published,draft

It will create a new model file. Blog can be replaced by any valid Python class name. I will create a file blog.py in models folder of core app

├── core
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models
│   │   ├── __init__.py
│   │   └── blog.py
│   ├── serializers
│   │   └── __init__.py
│   ├── tests.py
│   ├── urls.py
│   └── views
│       └── __init__.py

Generated code for model

screenshot

It will also register in the admin.py file core app. And export form models directory

make migrations
make migrate

Create Serializers and Viewset for your model

poetry run djinn generate core.Blog

It will create a new blog.py file in the views and serializers folder. Blog can be any valid model that already exists and registered.

├── core
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   ├── models
│   │   │   ├── __init__.py
│   │   │   └── blog.py
│   │   ├── serializers
│   │   │   ├── __init__.py
│   │   │   └── blog.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views
│   │       ├── __init__.py
│   │       └── blog.py

Generated code for serializer

screenshot

Generated code for view file screenshot

It will also register the viewset in the urls.py file of core app.

Todos (WIP)

  • Serializer generation
  • View generation
  • Url generation
  • Model generation
  • App generation
  • Add CLI for code generation
  • Add CLI for model generation
  • Add CLI for app generation
  • Google Oauth
  • Add docker support
  • Github Actions
  • Openapi for client generation
  • Setup guide
  • Add Environment variable file support
  • Add Docs for setting up and using the project
  • Add Cookie cutter
  • Resolve error (if file exists but the code class does not exist it does not generate new code)
  • Spin a separate package for code generator

If you some locales related error on wsl

sudo apt-get clean && sudo apt-get update
sudo apt-get install locales
locale-gen en_US.UTF-8

Releases

No releases published

Packages

No packages published

Languages