关于Django迁移出现问题
关于Django迁移出现问题
源码:
#coding:utf-8 from django.db import models # Create your models here. class BookInfo(models.Model): btitle = models.CharField(max_length=20) bpub_date = models.DateTimeField() class HeroInfo(models.Model): hname = models.CharField(max_length=10) hgender = models.BooleanField() hcontent = models.CharField(max_length=1000) # hbook = models.ForeignKey(BookInfo) hbook = models.ForeignKey('BookInfo', on_delete=models.CASCADE, )
出错:
(h2) fwl0000@ubuntu:~/桌面/project/test1$ python3 manage.py makemigrations Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/home/fwl0000/HOME/.virtualenvs/h2/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line utility.execute() File "/home/fwl0000/HOME/.virtualenvs/h2/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute django.setup() File "/home/fwl0000/HOME/.virtualenvs/h2/lib/python3.6/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/home/fwl0000/HOME/.virtualenvs/h2/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate app_config.import_models() File "/home/fwl0000/HOME/.virtualenvs/h2/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "/home/fwl0000/HOME/.virtualenvs/h2/lib/python3.6/importlib/__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 "/home/fwl0000/桌面/project/test1/booktest/models.py", line 11, in <module> class HeroInfo(models.Model): File "/home/fwl0000/桌面/project/test1/booktest/models.py", line 15, in HeroInfo hbook = models.ForeignKey(BookInfo) TypeError: __init__() missing 1 required positional argument: 'on_delete'
原因是本人安装的Django是2.0.7版本,但由于在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
TypeError: __init__() missing 1 required positional argument: 'on_delete'
举例说明:
user=models.OneToOneField(User)
owner=models.ForeignKey(UserProfile)
需要改成:
user=models.OneToOneField(User,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值
owner=models.ForeignKey(UserProfile,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值
参数说明:
on_delete有CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET()五个可选择的值
CASCADE:此值设置,是级联删除。
PROTECT:此值设置,是会报完整性错误。
SET_NULL:此值设置,会把外键设置为null,前提是允许为null。
SET_DEFAULT:此值设置,会把设置为外键的默认值。
SET():此值设置,会调用外面的值,可以是一个函数。
一般情况下使用CASCADE就可以了。
解决方案:
将第十一行的代码改为: hbook=models.ForeignKey('BookInfo',on_delete=models.CASCADE,) 即在外键值的后面加上 on_delete=models.CASCADE