简体   繁体   中英

how to resolve error in Django models.py?

I am new to Django and was trying to make a Django project inside the virtualenv to learn but getting following error on python manage.py make migrations :

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/suraj/Documents/my_projects/django_project/venv/lib/python3.6/site-packages/django/core/management/ init .py", line 381, in execute_from_command_line utility.execute()
File
"/home/suraj/Documents/my_projects/django_project/venv/lib/python3.6/site-packages/django/core/management/ init .py", line 357, in execute django.setup()
File
"/home/suraj/Documents/my_projects/django_project/venv/lib/python3.6/site-packages/django/ init .py", line 24, in setup apps.populate(settings.INSTALLED_APPS)
File
"/home/suraj/Documents/my_projects/django_project/venv/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate app_config.import_models()
File
"/home/suraj/Documents/my_projects/django_project/venv/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models self.models_module = import_module(models_module_name)
File
"/home/suraj/Documents/my_projects/django_project/venv/lib/python3.6/importlib/ init .py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed
File
"/home/suraj/Documents/my_projects/django_project/web_board/boards/models.py", line 11, in
class Topic(models.Model):
File
"/home/suraj/Documents/my_projects/django_project/web_board/boards/models.py", line 14, in Topic
board = models.ForeignKey(Board, related_name='topics')
TypeError: init () missing 1 required positional argument: 'on_delete'

Looks like you are creating a model with Foreign key .

When you create such model, you must specify what to happen when the record in foreign key table is deleted.

Just add what should happen when the record is deleted using on_delete parameter for that column in the model and you should be good.

board = models.ForeignKey(Board, related_name='topics', on_delete=**what_you_want_to_do_when_parent_record_gets_deleted**)

what does on_delete do on Django models?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM