Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
在django创建modules时出现以下错误
Failed to get real commands on module "blog_project": python process died with code 1: Traceback (most recent call last): File "D:\PyCharm 2016.3\helpers\pycharm\_jb_manage_tasks_provider.py", line 25, in <module> django.setup() File "D:\Python35\lib\site-packages\django-2.0-py3.5.egg\django\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "D:\Python35\lib\site-packages\django-2.0-py3.5.egg\django\apps\registry.py", line 112, in populate app_config.import_models() File "D:\Python35\lib\site-packages\django-2.0-py3.5.egg\django\apps\config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "D:\Python35\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 986, in _gcd_import File "<frozen importlib._bootstrap>", line 969, in _find_and_load File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 673, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 665, in exec_module File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed File "D:\pythonWorkStation\blog_project\blog\models.py", line 62, in <module> class Article(models.Model): File "D:\pythonWorkStation\blog_project\blog\models.py", line 69, in Article user = models.ForeignKey(User, verbose_name='用户') TypeError: __init__() missing 1 required positional argument: 'on_delete'
对应module的语句
1 class Article(models.Model): 2 title = models.CharField(max_length=50, verbose_name='文章标题') 3 desc = models.CharField(max_length=50, verbose_name='文章描述') 4 content = models.TextField(verbose_name='文章内容') 5 click_count = models.IntegerField(default=0, verbose_name='点击次数') 6 is_recommend = models.BooleanField(default=False, verbose_name='是否推荐') 7 date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间') 8 user = models.ForeignKey(User, verbose_name='用户') 9 category = models.ForeignKey(Category, blank=True, null=True, verbose_name='分类') 10 tag = models.ManyToManyField(Tag, verbose_name='标签') 11 12 objects = ArticleManager()
需要在第8行加上,on_delete=models.CASCADE,则问题解决
1 class Article(models.Model): 2 title = models.CharField(max_length=50, verbose_name='文章标题') 3 desc = models.CharField(max_length=50, verbose_name='文章描述') 4 content = models.TextField(verbose_name='文章内容') 5 click_count = models.IntegerField(default=0, verbose_name='点击次数') 6 is_recommend = models.BooleanField(default=False, verbose_name='是否推荐') 7 date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间') 8 user = models.ForeignKey(User, verbose_name='用户',on_delete=models.CASCADE,) 9 category = models.ForeignKey(Category, blank=True, null=True, verbose_name='分类',on_delete=models.CASCADE,) 10 tag = models.ManyToManyField(Tag, verbose_name='标签') 11 12 objects = ArticleManager() 13 14 class Meta: 15 verbose_name = '文章' 16 verbose_name_plural = verbose_name 17 ordering = ['-date_publish'] 18 19 def __unicode__(self): 20 return self.title