0

I am testing a very simple class in Django db :

class Airport(models.Model):
    code = models.CharField(max_length=3)
    city = models.CharField(max_length=64)

    def __str__(self):
        return f"{self.city} ({self.code})"

However, when makemigrations, it returns syntax error on the use of return f"{self.city} ({self.code})" :

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jello/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/jello/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/home/jello/.local/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/jello/.local/lib/python3.5/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/home/jello/.local/lib/python3.5/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 661, in exec_module
  File "<frozen importlib._bootstrap_external>", line 767, in get_code
  File "<frozen importlib._bootstrap_external>", line 727, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/mnt/c/Users/green/desktop/project3/orders/models.py", line 11
    return f"{self.city} ({self.code})"
                                      ^
SyntaxError: invalid syntax

When I modify the line with :

return '{} ({})'.format(self.city, self.code)

or

return self.city + "(" + self.code + ")"

Both works well. Anyone knows why the return f"{self.city} ({self.code})" gives error?

I am using Python version 3.5.2 and Django version 2.0.3

Thank you!

Cheryl
  • 3
  • 2
  • 2
    f-string syntax was added in [version 3.6](https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals). You're using 3.5 – xyres Mar 05 '20 at 08:49
  • 1
    Does this answer your question? [Python 3 returns "invalid syntax" when trying to perform string interpolation](https://stackoverflow.com/questions/42126794/python-3-returns-invalid-syntax-when-trying-to-perform-string-interpolation) – Hymns For Disco Mar 05 '20 at 08:51
  • Yes, after I upgraded to 3.6, it works! Thank you! – Cheryl Mar 05 '20 at 11:05

1 Answers1

0

It was happened because you are using python old version. F string support from python 3.6 version. So please update your python version. You haven't face any error

Ramen das
  • 16
  • 2