Django在根据models生成数据库表时报

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django在根据models生成数据库表时报相关的知识,希望对你有一定的参考价值。

D:TOOLPycharmProjectspython3BSh2>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:Program Filespython3.6libsite-packagesdjangocoremanagement__init__.py", line 371, in execute_from_command_line
utility.execute()
File "D:Program Filespython3.6libsite-packagesdjangocoremanagement__init.py", line 347, in execute
django.setup()
File "D:Program Filespython3.6libsite-packagesdjango
init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:Program Filespython3.6libsite-packagesdjangoapps egistry.py", line 112, in populate
app_config.import_models()
File "D:Program Filespython3.6libsite-packagesdjangoappsconfig.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "D:Program Filespython3.6libimportlib
init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:TOOLPycharmProjectspython3BSh2ooktestmodels.py", line 11, in <module>
class HeroInfo(models.Model):
File "D:TOOLPycharmProjectspython3BSh2ooktestmodels.py", line 15, in HeroInfo
hbook = models.ForeignKey(BookInfo)
**TypeError: init() missing 1 required positional argument: ‘on_delete‘


**
在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
TypeError: init() missing 1 required positional argument: ‘on_delete‘
解决方案:

class BookInfo(models.Model):
    btitle = models.CharField(max_length=20)
    bpub_date = models.DateTimeField()

class HeroInfo(models.Model):
    hname = models.CharField(max_length=20)
    hgender = models.BooleanField()
    hcontent = models.CharField(max_length=100)
    hbook = models.ForeignKey(‘BookInfo‘, on_delete=models.CASCADE)

https://www.cnblogs.com/phyger/p/8035253.html
技术分享图片

以上是关于Django在根据models生成数据库表时报的主要内容,如果未能解决你的问题,请参考以下文章

Django2.0在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'(示例

Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'

Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'

Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'

Django2.1在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'

Django在使用models生成数据库表时报错: __init__() missing 1 required positional argument: 'on_delete'