• 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.3helperspycharm\_jb_manage_tasks_provider.py", line 25, in <module>
        django.setup()
      File "D:Python35libsite-packagesdjango-2.0-py3.5.eggdjango\__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "D:Python35libsite-packagesdjango-2.0-py3.5.eggdjangoapps
    egistry.py", line 112, in populate
        app_config.import_models()
      File "D:Python35libsite-packagesdjango-2.0-py3.5.eggdjangoappsconfig.py", line 198, in import_models
        self.models_module = import_module(models_module_name)
      File "D:Python35libimportlib\__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:pythonWorkStationlog_projectlogmodels.py", line 62, in <module>
        class Article(models.Model):
      File "D:pythonWorkStationlog_projectlogmodels.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
  • 相关阅读:
    学期总结
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言博客作业04
    C语言I博客作业03
    C语言I博客作业02
    C语言I博客作业01
    学期总结
  • 原文地址:https://www.cnblogs.com/alighie/p/8118294.html
Copyright © 2020-2023  润新知