How to Backup or Duplicate Model Class Django Application in Microsoft Windows using inspectdb

Posted on

Introduction

This article has specific content for further description. It is about how to perform and simulate backup or duplicate the Django model. In other words, to show how to backup the python file containing model definition. The model definition exist from the database. So, after creating or migrating the model from a python file into the database, it possible to generate the python file containing model from the database. In other words, it is possible to reverse engineer the python file containing model from the database. If in a certain condition and situation where the python file containing model lost, corrupt or accidentally deleted, just regenerate it using the inspectdb tool. Below is the preparation for simulating the usage of ‘inspectdb’ tool to do it :

  1. It is necessary for the first step to have a Python interpreter tool. Check How to Install Python in Microsoft Windows, How to Install Python 3.10 in Microsoft Windows or How to Install Python in Microsoft Windows 11 to find out how to do it.

  2. Moreover, it is very important to have pip tool. Look at How to Install pip in Microsoft Windows or How to Install pip in Microsoft Windows 11 for a further reference.

  3. After having python and pip exist in the local device. Make sure to create a python virtual environment. For more info, look at How to Create an Isolated Python Environment, How to Create a Python Virtual Environment with a specific version of Python in Microsoft Windows or How to Specify Specific Python Version for Creating a Virtual Python Environment in Microsoft Windows

  4. After that, do not forget to active the python virtual environment. Install Django library using pip tool as in How to Install Django in Microsoft Windows to read as an example.

  5. Soon after Django library exist in the python virtual environment, create a Django project. Just use How to Create Django Project in Microsoft Windows using Command Line as an alternative info to do it.

  6. Furthermore, create a Django application inside the Django project using How to Create Django Project in Microsoft Windows using Command Line as reference.

That is all the preparation for simulating the backup or duplicate process of the python model file. Next, go through the following part to continue on doing it.

How to Backup or Duplicate Model Class Django Application in Microsoft Windows using inspectdb

So, as the python virtual environment currently active in the Django project root folder, perform the following command execution :

(env) C:\django\myproject>python manage.py inspectdb --database default auth_user > model_auth_user.py

(env) C:\django\myproject>

The above command will backup the model definition syntax of auth_user table which exist in the default database table definition into a file with the name of ‘model_auth_user.py’. In order to look for the database parameter, just check the settings.py file. Below is part of the settings.py file defining the ‘default’ database definition :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_myapp',
        'USER': 'postgres',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT':'5432',
    }
}

According to the above database definition, the ‘default’ database will connect to a database with the name of ‘db_myapp’ with the above configuration connection. Next,  for more info about how to run inspectdb, just execute the following command to list more information about it :

(env) C:\django\myproject>python manage.py inspectdb
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models

(env) C:\django\myproject>

The following is the content of the model_auth_user.py file :

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models

class AuthUser(models.Model):
password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
is_superuser = models.BooleanField()
username = models.CharField(unique=True, max_length=150)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
email = models.CharField(max_length=254)
is_staff = models.BooleanField()
is_active = models.BooleanField()
date_joined = models.DateTimeField()

class Meta:
managed = False
db_table = 'auth_user'

Leave a Reply