How to Remove Field in Django

Posted on

Basically, the article is written to show how to remove field specifically in a table which is represented by a class defined in a models.py file. The models.py is located in the root of application folder which is actually an application powered by Django framework. The models.py’s content is located as follows :

user@hostname:~/apps/apps$ tree -L 2
.
├── module
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
...

Below is the content of the file named models.py :

from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Module(models.Model):
    ...
    ...
    id_user = models.IntegerField(default=0, null=True)
    

    def __unicode__(self):
        return self.types +" - "+self.hostname

So, after removing the field or the actual attribute which is defined inside the class named Module which is representing the actual table, just execute the following command :

python manage.py makemigrations

The command itself, executed as follows :

user@hostname:~/apps/apps$ python manage.py makemigrations 
Migrations for 'module': 0003_remove_module_id_user.py: 
- Remove field id_user from module 
user@hostname:~/apps/apps$

The above command is only creating a specific script or file preparing for migration. The actual migration is actually done by executing the following command. So, don’t forget to execute the command the following after :

python mange.py migrate

The command executed as follows :

user@hostname:~/apps/apps$ python manage.py migrate
Operations to perform:
Apply all migrations: account, network, service, sessions, admin, sites, auth, server, contenttypes, socialaccount
Running migrations:
Applying network.0003_remove_module_id_user... OK
user@hostname:~/apps/apps$

So, in the above command, the actual script for migration has successfully executed. And then, after executing the above command, the actual field or column named ‘id_user’ will be completely removed from the actual table which is associated with the class defined in the models.py file. This is the actual table’s description before :

mysql> desc module;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| ...     |              |      |     |         |                |
| id_user | int(11)      | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

The following is the description of the table after the execution of the command :

mysql> desc module;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| ...     |              |      |     |         |                |
| id_user | int(11)      | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

Leave a Reply