Django2.0 build database tables based on models Times __init__ () Missing 1 required positional argument: ' On_delete '

Source: Internet
Author: User

1, cause: After creating the foreign key, execute Python manage.py makemigrations error, the specific code is:

 fromDjango.dbImportModelsclassClasses (models. Model): Title= Models. Charfield (max_length=32) M= Models. Manytomanyfield ("Teachers")classTeachers (models. Model): Name= Models. Charfield (max_length=32)classStudent (models. Model): Username= Models. Charfield (max_length=32) Age=models. Integerfield () Gender=models. Booleanfield () CS= Models. ForeignKey ("Classes")

2, the terminal display error is:

D:\pythonstudy\django_demo>python manage.py makemigrationstraceback (most recent call last): File"manage.py", line 15,inch<module>execute_from_command_line (SYS.ARGV) File"C:\Users\huiYan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init_ _.py", line 371,inchexecute_from_command_line utility.execute () File"C:\Users\huiYan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init_ _.py", line 347,inchExecute Django.setup () File"C:\Users\huiYan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 24,inchSetup apps.populate (settings. Installed_apps) File"C:\Users\huiYan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 112,inchpopulate App_config.import_models () File"C:\Users\huiYan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\config.py", line 198,inchimport_models Self.models_module=import_module (models_module_name) File"C:\Users\huiYan\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126,inchImport_modulereturn_bootstrap._gcd_import (name[level:], package, level) File"<frozen importlib._bootstrap>", line 978,inch_gcd_import File"<frozen importlib._bootstrap>", line 961,inch_find_and_load File"<frozen importlib._bootstrap>", line 950,inch_find_and_load_unlocked File"<frozen importlib._bootstrap>", Line 655,inch_load_unlocked File"<frozen importlib._bootstrap_external>", Line 678,inchExec_module File"<frozen importlib._bootstrap>", line 205,inch_call_with_frames_removed File"D:\pythonstudy\Django_demo\app01\models.py", Line 18,inch<module>classStudent (models. Model): File"D:\pythonstudy\Django_demo\app01\models.py", line 22,inchStudent CS= Models. ForeignKey ("Classes")TypeError:  __init__() Missing 1 required positional argument: 'on_delete' 

3. Solution:

Add On_delete=models after the foreign key. CASCADE, the error disappears.

  CS = models. ForeignKey ("Classes", On_delete=models. CASCADE,)

Django2.0 generate database tables based on models Times __init__ () Missing 1 required positional argument: ' On_delete '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.