DR-CLI: A Flexible Cookie cutter and CRUD Endpoints Generator for Django
Tags: django, tooling, python
Published: 2022-05-13 15:50:21

I've been writing REST APIs with the Django framework for some time now. Over the years, I couldn't help but notice, with discontent, how I've been repeating certain things in every project. 

And there were things I just wished could be better. Permit me to kick this off with a catalogue of some of the issues I felt needed fixing :)

**Problem-1:** First of all, I noticed I was repeating at least one of the following integrations/features in every project:

 - A custom user model defined in a `users` app
 - Authentication endpoints.
 - [python-decouple](https://pypi.org/project/python-decouple/): for managing environment variables 
 - [drf-spectacular](https://drf-spectacular.readthedocs.io/en/latest/readme.html): for auto-generating API docs 
 - [dj-database-url](https://github.com/jazzband/dj-database-url): for connecting to various databases 
 - [pre-commit, black, isort, flake8](https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/): for code linting with pre-commit hooks 
 - [pytest-django](https://pytest-django.readthedocs.io/en/latest/): for writing unit tests with pytest 
 - [Docker](https://learndjango.com/tutorials/django-docker-and-postgresql-tutorial): for containerization.

At a point, I wished there was a tool that could start my project with the integrations I need already configured. I found [cookie-cutter-django-rest](https://github.com/agconti/cookiecutter-django-rest) to be useful, but sometimes it's just an overkill for my use-case. 

I needed something more flexible. Something that could start my project with a few or all the integrations above as the case may be.

**Problem-2:** Furthermore, I also realized I was always creating CRUD endpoints for most of the database models I define. CRUD endpoints like authentication are mainstay of most projects. 

But most importantly, CRUD endpoints are so nondescript that I wish there was a tool to automate the task of writing them myself. 

This type of automation I believe is necessary as it would save me minutes and perhaps hours(when dealing with hundreds of models) spent on the mundane CRUD stuff :(

**Problem-3:** Lastly, I had always wish there was a way to create multiples apps at once in my Django projects. I felt the need for this feature because most times I tend to know all the apps in my project at the design phase.

Overall, I was just dissatisfied with the time spent configuring third party packages, and writing CRUD endpoints when I could channel that energy into the more exciting parts of my projects.

To solve my problems, I scoured GitHub and PYPI for that one tool forged with the "super powers" to start my project with the integrations I need already configured as well as the potential to generate CRUD endpoints for the models I define with just a command.

My hunt ended rather disappointingly, and I figured well, maybe someone needs to create that tool. 

## Enter Django Rest CLI(dr-cli)

![Start Project with Django Rest CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vchsl487bunon2b65tj3.PNG)

We created dr-cli to be a tool that comes bundled with:

- The capability to start your project with the integrations you need already configured. For example: authentication endpoints with dj-rest-auth, auto-generated docs with drf-spectacular, docker support, and more.
- The potential to generate CRUD endpoints for the models you define. For example, if you define a model, **Product** in your `models.py` file, this tool could generate a _GET /products POST /products PUT /products/<product_id>_ etc. endpoints for that model.
- An ability to create multiple apps at once in your project.

Cool, how can I get started with this?

## Getting Started

### Installation
```pip install dr-cli```
I highly recommend that you install this in a virtual environment.

### Create a New Project
```Run dr-cli startproject project_name```
 
You'd be prompted to start your project from one of three templates: **Basic, Medior, and Advanced** templates. Learn more about what each template comes bundled with [here](https://github.com/py-universe/django-rest-cli/blob/docs/templatesInfo.md).

On selecting one of the templates your project will then be created. Git will be initialized in your project, and all project dependencies installed.

The generated project comes with a nice Readme containing the steps for running the project.

### Create New Apps in your Project
```Run dr-cli startapps todo me-nu user``` Where todo, me-nu, and user are the app names.

On running the above command, name validations would be performed first, and then all apps that pass the validation would be created.

**Note:** Make sure to add your created apps to the list of INSTALLED APPS

### Generate CRUD Endpoints for your Apps
```Run dr-cli addcrud memo user``` to create CRUD endpoints for the models defined in the memo & user apps, for example.

**Note:** Make sure to register the URLs for each app in the top level urls.py file.

### Accessing the docs page
- Run ```python manage.py runserver``` to fire up your local development server, and point your browser to `http://localhost:8000/api/v1/docs` to view the auto-generated docs page similar to the one shown in the image below:

![Auto Generated Docs with dr-cli](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xj2kjublph85bb3e59i4.PNG)


You can visit the [project's repository](https://github.com/py-universe/django-rest-cli) to see a nice demo demonstrating these commands.

We are seeking ways to improve our current implementation. We'd really be happy to hear your thoughts. You can find me on [LinkedIn](https://www.linkedin.com/in/nyior/), [Twitter](https://twitter.com/nyior_clement) or [GitHub](https://github.com/Nyior).