问题:在执行python manage.py makemigrations learning_logs时,系统会报错,提示:TypeError: __init__() missing 1 required positional argument: 'on_delete'

(ll_env) c:\WorkSpace\SimpleTest\learning_log>python manage.py makemigrations learning_logs
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "c:\WorkSpace\SimpleTest\learning_log\ll_env\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "c:\WorkSpace\SimpleTest\learning_log\ll_env\lib\site-packages\django\core\management\__init__.py", line 347, in execute
    django.setup()
  File "c:\WorkSpace\SimpleTest\learning_log\ll_env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "c:\WorkSpace\SimpleTest\learning_log\ll_env\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "c:\WorkSpace\SimpleTest\learning_log\ll_env\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\python36\lib\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 "c:\WorkSpace\SimpleTest\learning_log\learning_logs\models.py", line 13, in <module>
    class Entry(models.Model):
  File "c:\WorkSpace\SimpleTest\learning_log\learning_logs\models.py", line 15, in Entry
    topic = models.ForeignKey(Topic)
TypeError: __init__() missing 1 required positional argument: 'on_delete'

代码如下:

from django.db import models

# Create your models here.
class Topic(models.Model):
    """用户学习的主题"""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)
   
    def __str__(self):
        """返回模型的字符串表示"""
        return self.text

class Entry(models.Model):
    """学到的有关某个主题的具体知识"""
    topic = models.ForeignKey(Topic)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)
   
    class Meta:
        verbose_name_plural = 'entries'
   
    def __str__(self):
        """返回模型的字符串表示"""
       
        return self.text[:50] + "..."

解决办法:修改指定外键的方式

from django.db import models

# Create your models here.
class Topic(models.Model):
    """用户学习的主题"""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)
   
    def __str__(self):
        """返回模型的字符串表示"""
        return self.text

class Entry(models.Model):
    """学到的有关某个主题的具体知识"""
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)
   
    class Meta:
        verbose_name_plural = 'entries'
   
    def __str__(self):
        """返回模型的字符串表示"""
       
        return self.text[:50] + "..."

Django问题 TypeError: __init__() missing 1 required positional argument: 'on_delete'的更多相关文章

  1. Django关联数据库时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    sgrade = models.ForeignKey("Grades",) 执行python manage.py makemigrations后出现TypeError: __ini ...

  2. Django :执行 python manage.py makemigrations 时报错 TypeError: __init__() missing 1 required positional argument: 'on_delete'

    原因 执行命令 python manage.py makemigrations 报错 TypeError: __init__() missing 1 required positional argum ...

  3. 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...

  4. python进行数据库迁移的时候显示(TypeError: __init__() missing 1 required positional argument: 'on_delete')

    进行数据库迁移的时候,显示  TypeError: __init__() missing 1 required positional argument: 'on_delete' 图示: 出现原因: 在 ...

  5. Django 生成数据库表时的报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    原因及解决办法: https://www.cnblogs.com/phyger/p/8035253.html

  6. 解决TypeError: __init__() missing 1 required positional argument: 'on_delete'

    试用Djiango的时候发现执行mange.py makemigrations 和 migrate是会报错,少位置参数on_delete,查了一下是因为指定外键的方式不对,改一下就OK了. 代码如下: ...

  7. 【python3】 django2.0 在生成数据库表时报错: TypeError: __init__() missing 1 required positional argument: 'on_delete'

    python: 3.6.4 django: 2.0 models.py 代码如下 # coding: utf-8 from django.db import models from django.co ...

  8. TypeError: __init__() missing 1 required positional argument: 'on_delete'

    报错的原因呢,就是在设计model时我弄了个外键,然后就报错了... 不难看出,它是想让我们在表与表关联时添加一个on_delete参数 解决办法: 如其所愿,加上on_delete=models.C ...

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

    from django.db import models # Create your models here. class Category(models.Model): caption = mode ...

随机推荐

  1. 前端开发工程师 - 01.页面制作 - 第4章.CSS

    第4章.CSS CSS简介 Cascading Style Sheet 层叠样式表:定义页面中的表现样式 history: CSS1(1996)--CSS2(1998)--着手CSS3草案(拆分成很多 ...

  2. Liunx 基本命令

    find : find ./ -name "*instantiate_post_check.yml*" grep: openstack network show fe92bfcf- ...

  3. Matlab带比较方法的快排

    首先是主方法QUCIKSORT:(从小到大排列) function [A]=QUICKSORT(A,Low,high,mdat) set(,) if Low<high [A,w]=SPLITIO ...

  4. crt0.S(_main)代码分析

    crt0,S(_main)代码分析 --- 1. 设置sp寄存器地址 //设置SP栈指针 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG ...

  5. wpa_supplicant下行接口浅析

    wpa_supplicant通过socket通信机制实现下行接口,与内核进行通信,获取信息或下发命令. 以下摘自http://blog.csdn.net/fxfzz/article/details/6 ...

  6. Notes of the scrum meeting before publishing(12.17)

    meeting time:18:30~20:30p.m.,December 17th,2013 meeting place:3号公寓一层 attendees: 顾育豪                  ...

  7. ACM 第八天

    数据结构和算法目录表 数据结构和算法目录表   C C++ Java 线性结构 1. 数组.单链表和双链表 2. Linux内核中双向链表的经典实现  数组.单链表和双链表  数组.单链表和双链表   ...

  8. Windows平台下安装.net coreclr

    .net coreclr 已经发布RC1版本,安装方法如下: 1.安装DNVM,DNVM是.net运行时管理器,负责管理所有版本的.net运行时(.net framework..net coreclr ...

  9. lintcode-141-x的平方根

    141-x的平方根 实现 int sqrt(int x) 函数,计算并返回 x 的平方根. 样例 sqrt(3) = 1 sqrt(4) = 2 sqrt(5) = 2 sqrt(10) = 3 挑战 ...

  10. iOS- UIPickerView餐厅点餐系统

    在餐厅里的点餐系统的核心控件就是UIPickerView 今天晚上在整理以前的项目笔记时,特意把UIPickerView单独拿出来,做了一个简陋的点餐道具. 因为没有素材图片,所有大家将就看看吧 0. ...