How to Solve Error Message Model Attribute Problem SyntaxError: invalid syntax in Django Application

Posted on

Introduction

Another error appear when there is an execution of a Django-based internal service. The error actually appear after editing a model definition. The definition of the model actually exist in the file ‘models.py’. The following is an actual execution of the Django-based internal service :

C:\programming\python\django\project\org\models.py changed, reloading.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\app\python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\app\python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\app\python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "C:\app\python39\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\app\python39\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "C:\app\python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\app\python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\app\python39\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\app\python39\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\app\python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 846, in exec_module
File "<frozen importlib._bootstrap_external>", line 983, in get_code
File "<frozen importlib._bootstrap_external>", line 913, in source_to_code
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\programming\python\django\myproject\org\models.py", line 31
[product code],
^
SyntaxError: invalid syntax
</pre>

So, what is the line which is causing the error of ‘SyntaxError: invalid syntax’. Apparently, it is the definition of one of the attribute in the model exist in the models.py file. In order to declare or to define an attribute of a model, there are certain rules which is very important to follow. Those are the rules for using the combination of number and letter with either one of them become the first character for the name of the attribute. So, the solution for solving the problem of invalid syntax is just revise the attribute name. The following is the actual definition of the atribute’s model :

[product code]= models.CharField(max_length=255, blank=True, null=True)

Modify it as follows :

product_code = models.CharField(max_length=255, blank=True, null=True)

Execute the command for starting the Django-based application once more. If there are no further error message, the Django-based internal service will run properly. Moreover, it will run normally upon executing to generate migration script and implement it into the database.

Leave a Reply