How to Rename Application in Django

Posted on

In Django framework, in order to rename the application which is already created is very easy. The process is executed by renaming the folder which is representing the application. The following is the actual information concerning on the application :

1. There is an already running application named ‘apps’.

2. The application is going the renamed into another name. The name of the application is ‘application’.

3. Renaming application can be done by executing the following command :

mv apps application

It can be shown as follows :

user@hostname:~/project/apps$ mv apps application
user@hostname:~/project/apps$

The following is the actual folder location which is taken from the project folder :

user@hostname:~/project/apps$ tree -L 2
.
├── db.sqlite3
├── manage.py
├── application
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── module
    ├── admin.py
    ├── admin.pyc
    ├── apps.py
    ├── apps.pyc
    ├── __init__.py
    ├── __init__.pyc
    ├── migrations
    ├── models.py
    ├── models.pyc
    ├── static
    ├── templates
    ├── tests.py
    ├── urls.py
    ├── urls.pyc
    ├── views.py
    └── views.pyc
5 directories, 23 files
user@hostname:~/project/apps$ 

4. But the consequences is every table which is represented inside the associated application must be changed. For instance, if there is a class named ‘task’ defined in a model file named models.py as shown below :

from __future__ import unicode_literals
from datetime import datetime
from django.db import models
# Create your models here.
class Task(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()
created_at = models.DateTimeField(default=datetime.now)
def __str__(self):
return self.title

The above class named ‘Task’, previously it is located in an application named ‘apps’ and it is changed into an application named ‘application’. It is made just to explain the scenario. Below is the actual step which is actually taken to change the table name. This is an obvious step because executing the command ‘python manage.py makemigrations’ and ‘python manage.py migrate’ to create a table which is represented by a class defined in the models.py will actually takes the application name as part of the table name. So, in order to make the application work perfectly after renaming it, just re-execute the command and the table will also be renamed. The pattern of the table is ‘appname_classname’. Below is the example of it :

user@hostname:~/project/apps$ python manage.py migrate
Operations to perform:
Apply all migrations: account, network, service, sessions, admin, sites, auth, server, contenttypes, socialaccount
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
The following content types are stale and need to be deleted:
application | apps
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel: yes
user@hostname:~/project/apps$

One thought on “How to Rename Application in Django

Leave a Reply