Django is currently my favorite framework when it comes to building web applications. I was first introduced to Django during my course CS50’s Web Programming with Python and JavaScript.

It’s a fully featured framework, that comes with pretty much everything one would need for a modern web application. But what’s even more important is, the framework is very flexible and customizable. And just like for Python, there is a huge network of 3rd party packages and huge community around Django.

I have been trying various things with Django, built few projects, but each time I use the command django-admin startproject mysite a whole new world opens and I still discover new ways of doing things. One really needs to stay humble and be open to learn from others.

My largest project so far is the app Books. I was working on this app for a few months on my own and while trying to solve various problems in order to make the app work I realized just how huge and rich the Django’s environment is.

Now looking back, I would write most of the code differently, but this was of course my first bigger project and still during my early stages of learning programming. Not that I would write a perfect app now, far from it. Maybe a little bit better, but I still have so much to learn.

As I am exploring this amazing framework, I am realizing there are best practices, standards that are recommended by developers that have been using Django for a very long time. And that one should listen to these people.

I want to share some of these things I have learned along the way. One that comes to mind right now as I am starting new project is regarding the built-in Custom User Model. I remember I wasted a lot of time researching this problem during the development of my Books app when I changed, later in the development, backend model for user authentication and now I know why.

Django comes with default User model that has already plenty of options, but you should always think ahead and make your app/program as flexible and easy to change as possible. It can happen, like it happened to me, that you decide to change something about User model in the future.

For this reason, Django and the community recommends you to build custom user model that inherits from deafult AbstractUser model immediately after starting new project.

1
2
3
4
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass

You should also point AUTH_USER_MODEL to it and do this before creating any migrations or running manage.py migrate for the first time.

Simple thing that can avoid a lot of problems down the road.