This article main concern is to show how to solve a problem upon executing ‘python manage.py makemigrations’ command. It is a command for migrating or implementing model into associated table in the database definition. The database definition itself exist in the ‘settings.py’ file configuration of the Django application. But unfortunately, after creating the model in the ‘models.py’ upon executing ‘python manage.py makemigrations’, there is an error occurs. First of all, below is the detail of the error messages :
user@hostname:~/python/django/todoproject$ source /home/user/python/django/env/bin/activate (env) user@hostname:~/python/django/todoproject$ python manage.py makemigrations Traceback (most recent call last): File "manage.py", line 21, in main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/home/user/python/django/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute django.setup() File "/home/user/python/django/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/home/user/python/django/env/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate app_config.import_models() File "/home/user/python/django/env/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models self.models_module = import_module(models_module_name) File "/home/user/python/django/env/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 967, in _find_and_load_unlocked File "", line 677, in _load_unlocked File "", line 724, in exec_module File "", line 860, in get_code File "", line 791, in source_to_code File "", line 219, in _call_with_frames_removed File "/home/user/python/django/todoproject/todoapp/models.py", line 24 class Meta: ^ IndentationError: unexpected indent (env) user@hostname:~/python/django/todoproject$
As in the above output, the error above actually happened because of the wrong placement of the ‘class Meta:’ position. It is already clear in the error message output. So, what is actually the actual source code looks like ?. The following is the content of the ‘models.py’ :
from django.db import models from django.utils import timezone # Create your models here. class Category(models.Model): # The Category table name that inherits models.Model name = models.CharField(max_length=100) #Like a varchar class Meta: verbose_name = ("Category") verbose_name_plural = ("Categories") def __str__(self): return self.name #name to be shown when called class TodoList(models.Model): #Todolist able name that inherits models.Model title = models.CharField(max_length=250) # a varchar content = models.TextField(blank=True) # a text field created = models.DateField(default=timezone.now().strftime("%Y-%m-%d")) # a date due_date = models.DateField(default=timezone.now().strftime("%Y-%m-%d")) # a date category = models.ForeignKey(Category, default="general") # a foreignkey class Meta: ordering = ["created"] # ordering by the created field def __str__(self): return self.title #name to be shown when called
As the information in the output error message exist, there is an error in line 24 where it is actually where the ‘class Meta:’ source code part exist. The indentation is wrong so it is generating an error. The previous ‘class Meta:’ in the first model definition is actually is in the right position. Therefore, in order to solve the problem, just place the ‘class Meta:’ source code part in the same indentation with the previous ‘class Meta:’ source code part in the first model. Below is the revision of the source code :
from django.db import models from django.utils import timezone # Create your models here. class Category(models.Model): # The Category table name that inherits models.Model name = models.CharField(max_length=100) #Like a varchar class Meta: verbose_name = ("Category") verbose_name_plural = ("Categories") def __str__(self): return self.name #name to be shown when called class TodoList(models.Model): #Todolist able name that inherits models.Model title = models.CharField(max_length=250) # a varchar content = models.TextField(blank=True) # a text field created = models.DateField(default=timezone.now().strftime("%Y-%m-%d")) # a date due_date = models.DateField(default=timezone.now().strftime("%Y-%m-%d")) # a date category = models.ForeignKey(Category, default="general") # a foreignkey class Meta: ordering = ["created"] # ordering by the created field def __str__(self): return self.title #name to be shown when called
The above part of source is a modification where there is a correction on the indentation of ‘class Meta:’ along with the ‘def __str__(self):’ source code part. Finally, re-execute again to migrate the database of the application. Actually, the above example is exist in an article with the title of ‘How to Build a Todo App with Django’ in this link.