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
- Authentication endpoints.
- python-decouple: for managing environment variables
- drf-spectacular: for auto-generating API docs
- dj-database-url: for connecting to various databases
- pre-commit, black, isort, flake8: for code linting with pre-commit hooks
- pytest-django: for writing unit tests with pytest
- Docker: 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 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)
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.pyfile, this tool could generate a GET /products POST /products PUT /products/ etc. endpoints for that model.
- An ability to create multiple apps at once in your project.
Cool, how can I get started with this?
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.
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
python manage.py runserverto fire up your local development server, and point your browser to
http://localhost:8000/api/v1/docsto view the auto-generated docs page similar to the one shown in the image below:
You can visit the project's repository 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, Twitter or GitHub.
Top comments (1)
Looking to try this out this week 👌🏾💥⚡