分类:Python 新编程

python TypeError: __init__() missing 1 required positional argument: 'on_delete'解决方案

在调试Django的时候发现执行python manage.py makemigrations 和 migrate是会报错,少位置参数on_delete

执行命令如下:

python manage.py makemigrations

报错提示如下:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Administrator\Envs\py_django\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\Administrator\Envs\py_django\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\Users\Administrator\Envs\py_django\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Administrator\Envs\py_django\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\Administrator\Envs\py_django\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\users\administrator\appdata\local\programs\python\python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Administrator\Envs\py_django\test1\booktest\models.py", line 11, in <module>
    class HeroInfo(models.Model):
  File "C:\Users\Administrator\Envs\py_django\test1\booktest\models.py", line 15, in HeroInfo
    book = models.ForeignKey(BookInfo)
TypeError: __init__() missing 1 required positional argument: 'on_delete'

是什么原因呢?看到培训的视频,按照老师的去敲代码,发现老师的可以正常执行,为什么自己的不行呢?

细心的同学会发现老师的Python 2.x的版本,现在的版本是Python 3.x了

出错代码如下:

from django.db import models


# Create your models here.


class BookInfo(models.Model):
    """model of book infomation"""

    # 书的标题 字符串类型  最大长度20
    btitle = models.CharField(max_length=20)

    # 出版日期 日期类型
    bpub_date = models.DateTimeField()


class HeroInfo(models.Model):
    """model of hero information"""

    # 英雄的名字  字符串类型 最大长度30
    hname = models.CharField(max_length=30)

    # 性别 布尔类型
    hgender = models.BooleanField()

    # 内容 字符串类型 最大长度1000
    hcontent = models.CharField(max_length=1000)

    # 英雄出现的书 一对多设计 多方持有一方的外键
     hbook = models.ForeignKey(BookInfo)

解决方案如下:

   # 英雄出现的书 一对多设计 多方持有一方的外键
    # hbook = models.ForeignKey(BookInfo)
    hbook = models.ForeignKey('BookInfo', on_delete=models.CASCADE)

再次执行命令的

python manage.py migrate

显示结果如下:正常显示

Operations to perform:
  Apply all migrations: admin, auth, booktest, contenttypes, sessions
Running migrations:
  No migrations to apply.

分类:Python 新编程