Tips Before Starting a Django Project

Farhan Khan
Cashify Engineering
5 min readOct 3, 2018

--

Often in the middle of a large project, we come across things that should have been taken care of at the very beginning. Here at Cashify, we came across many such things that should have been done before starting a project. Hence, we would like to share our experience so that others do not face the same frustration we did.

The picture below pretty much depicts our condition during many situations.. ;)

So, here are some tips that I have summoned that you might find useful before you start your project in Django:

1. Project Layout

In the default Django project setup — views, models and forms are placed in views.py, models.py and forms.py respectively in an app directory. However, as your project grows larger, managing hundreds of models, views etc. in a single file becomes cumbersome. So it is better to place your models, views, forms and admin files in separate directories named models, views, forms and admin.

Inside these directories, place __init__.py that tells Django which files to pick. A typical structure looks like this :

django project layout.

If you are starting fresh and have an existing database, then this simple management command built on top of inspectdb will help you a lot. It will automatically create the required directory structure, and the model’s files will be pre-populated. You can thank me afterwards…. ;)

2. Custom User Model

Many times you will need to override the auth user model that comes with the Django authentication backend. For example, you would like to add some fields to the auth user table or change the existing ones. Changing the user model mid-project becomes a tedious task as it may lead to migration errors.

Django’s documentation on this suggests the same. It is always better to start with a custom user model, so that we can change it as the need arises in the future. To substitute your user model:

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
pass

Then point to the user model in settings.py. Do this before creating any migrations or running manage.py migrate for the first time. Also, register your user model in your admin.

AUTH_USER_MODEL = 'your_app.CustomUser'

So whenever the need arises you can modify the user model and run migrations to update the table.

3. Managing your settings.py

It is equally important to manage your settings.py. You might have different features for your project depending on the environment. You might be testing some features on the testing server which will go to staging and finally on production. As a result, there could be differences in the dependencies. So, it is better to break your settings:

breaking your settings.py

In your main settings.py you can import from other settings_env_name.py as:

try:    import importlib
env_mode = env('MODE', default='local')
filename = "your_project.settings.settings_%s" % (env_mode)
module = importlib.import_module(filename)
names = [name for name in dir(module) if not name.startswith('_')]
globals().update({name: getattr(module, name) for name in names})
except:
pass

Here MODE is the name of the environment variable which has a value such as beta, local, test, prod etc. Since your settings.py is inside a directory now, make sure you change the BASE_DIR to:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

4. Using Virtual Environment

When we start any project, we install other dependencies based on our requirements. Now the problem arises when we try to deploy the project on the server. So it is always better to have a list of all the dependencies that are to be installed.

Moreover, if we have many projects in our system, it is better to keep their dependencies separate, so that they do not affect each other in the future in case of an upgrade or version change. For example, some old projects may be using Django 1.11. In such a scenario, you may want to try out Django 2.0 without affecting these projects.

So if you are using python3 and Ubuntu, then follow these steps:

sudo pip3 install virtualenv # to install virtualenv

Now create a virtual environment using the following command:

virtualenv -p python3 your_env_name

This will create a virtual environment with the name provided. To activate it:

source your_env_name/bin/activate

Now start installing your dependencies, and whenever you want to deploy it, you can list all the dependencies using pip freeze.

pip freeze > requirements.txt

It will save the output of pip freeze in a file named — requirements.txt. Now pip can also install the packages in the file using -r flag:

pip install -r requirements.txt

This will then install all the dependencies listed in the file.

You may even have a different requirements.txt based on the environment just like settings.py:

different dependency files.

5. Using Django Debug Toolbar

This is an excellent and useful utility which helps to keep track of the queries which are going to the database. It helps us to know the SQL query made by the django ORM behind the scenes. This is useful to identify slower SQL queries which need optimization.

It is particularly helpful when you are using the Django admin panel because in the admin panel, you are unaware of the queries being made by django. It’s all magic going on behind the scenes. But as you grow and the data grows exponentially, the panel might become slow. So better take care of it now, rather than facing problems afterward.

Conclusion:

So in this article, we saw some things which we should do before starting any django project. These were based on my experience working at Cashify. I will try adding more points to this article in future.

--

--