Django how to reset app table using migration


This is simple tricks to reset app table or solve default field value.

1. Commented all app below your app in settings.py

2. Commented your app model

3. Makemigration to generate migration that delete your app table

4. Un-comment and add modification

5. Makemigration to generate migration that create your app table

Example:

I have app “insurance”. So, first, i disable all app below insurance in settings APP :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Application definition
INSTALLED_APPS = (
    ‘grappelli’,
    ‘django.contrib.admin’,
    ‘django.contrib.auth’,
    ‘django.contrib.contenttypes’,
    ‘django.contrib.sessions’,
    ‘django.contrib.messages’,
    ‘django.contrib.staticfiles’,
    ‘django_extensions’,
    ‘widget_tweaks’,
    ‘bootstrap_pagination’,
    ‘haystack’,
    ‘insurances’,
#    ‘territorials’,
#    ‘companystructures’,
)

2. Then I comment my insurance models.py and admin.py (if any)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CommonInfo(StripModelMixin, models.Model):
    remarks = models.TextField(blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    status = models.BooleanField(default=True)

    class Meta:
        abstract = True

#class Insurance(CommonInfo):
#    name = models.CharField(max_length=255)
#    code = models.CharField(max_length=255, unique=True)
#
#    uuid = UUIDField(auto=True)

3. Make migrations to delete this table

1
python manage.py makemigrations insurances

4. Now my insurances table already deleted. Then we can re-generate new insurances models.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CommonInfo(StripModelMixin, models.Model):
    remarks = models.TextField(blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    status = models.BooleanField(default=True)

    class Meta:
        abstract = True

class Insurance(CommonInfo):
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=255, unique=True)
    users= models.ForeignKey(Users)
    uuid = UUIDField(auto=True)
1
python manage.py makemigrations insurances

Migrate now!

5. Un-comment all app in settings.py and voila!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.