nishchay trivedi
nishchay trivedi

Reputation: 75

sync database to python django

I am trying to connect the SQLite3 database to Django. while I try to sync the database with a command

$ python manage.py syncdb

it shows a long trace. I don't know how to fix it. can anyone help me with that

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 985, in _gcd_import
  File "<frozen importlib._bootstrap>", line 968, in _find_and_load
  File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 697, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "F:\django\DatabaseConnectTest\polls\models.py", line 9, in <module>
    class Choice(models.Model):
  File "F:\django\DatabaseConnectTest\polls\models.py", line 10, in Choice
    quesion = models.ForeignKey(Question)
TypeError: __init__() missing 1 required positional argument: 'on_delete'

Upvotes: 2

Views: 961

Answers (1)

H&#229;ken Lid
H&#229;ken Lid

Reputation: 23064

The on_delete argument is required on all ForeignKey fields since Django 2.0. There's no longer a default value. You have to go through all your models and add this argument to relations.

The traceback will tell you exactly where to find the code that should be fixed.

File "F:\django\DatabaseConnectTest\polls\models.py", line 10, in Choice
quesion = models.ForeignKey(Question)

In most cases on_delete=models.CASCADE is what you want.

question = models.ForeignKey(Question, on_delete=models.CASCADE)

https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey.on_delete

Upvotes: 2

Related Questions