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 '