大 白老鼠
2019-12-29 19:39
采纳率: 50%
浏览 1.4k
已采纳

执行 python manage.py makemigrations 提示错误

Traceback (most recent call last):
File "manage.py", line 23, in
main()
File "manage.py", line 19, in main
execute_from_command_line(sys.argv)
File "D:\codeProgram\python3.7\lib\site-packages\django\core\management__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:\codeProgram\python3.7\lib\site-packages\django\core\management__init__.py", line 357, in execute
django.setup()
File "D:\codeProgram\python3.7\lib\site-packages\django__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\codeProgram\python3.7\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "D:\codeProgram\python3.7\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "D:\codeProgram\python3.7\lib\importlib__init__.py", line 127, in import_module
return bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "", line 728, in exec_module
File "", line 219, in _call_with_frames_removed
File "D:\codeStudy\django企业开发实战\typeidea\typeidea\blog\models.py", line 8, in
class Category(models.Model):
File "D:\codeStudy\django企业开发实战\typeidea\typeidea\blog\models.py", line 20, in Category
owner = models.ForeignKey(User, verbose_name="作者")
TypeError: __init
_() missing 1 required positional argument: 'on_delete'

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib.auth.models import User
from django.db import models


class Category(models.Model):
    STATUS_NORMAL = 1
    STATUS_DELETE = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, '正常'),
        (STATUS_DELETE, '删除'),
    )

    name = models.CharField(max_length=50, verbose_name="名称")
    status = models.PositiveIntegerField(
        default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    is_nav = models.BooleanField(default=False, verbose_name="是否为导航")
    owner = models.ForeignKey(User, verbose_name="作者")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = verbose_name_plural = '分类'


class Tag(models.Model):
    STATUS_NORMAL = 1
    STATUS_DELETE = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, '正常'),
        (STATUS_DELETE, '删除'),
    )

    name = models.CharField(max_length=10, verbose_name="名称")
    status = models.PositiveIntegerField(
        default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    owner = models.ForeignKey(User, verbose_name="作者")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = verbose_name_plural = '标签'


class Post(models.Model):
    STATUS_NORMAL = 1
    STATUS_DELETE = 0
    STATUS_DRAFT = 2
    STATUS_ITEMS = (
        (STATUS_NORMAL, '正常'),
        (STATUS_DELETE, '删除'),
        (STATUS_DRAFT, '草稿'),
    )

    title = models.CharField(max_length=255, verbose_name="标题")
    desc = models.CharField(max_length=1024, blank=True, verbose_name="摘要")
    content = models.TextField(verbose_name="正文", help_text="正文必须为MarkDown格式")
    status = models.PositiveIntegerField(
        default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    category = models.ForeignKey(Category, verbose_name="分类")
    tag = models.ManyToManyField(Tag, verbose_name="标签")
    owner = models.ForeignKey(User, verbose_name="作者")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = verbose_name_plural = "文章"

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 幸福丶如此 2019-12-30 10:02
    已采纳

    django新版本中,如果有外键关联,必须要指定 on_delete=on_delete是当删除关联表中的数据时,当前表与其关联的行的行为
    可设置的值有

    on_delete=None,             # 当删除关联表中的数据时,当前表与其关联的行的行为
                                            - models.CASCADE,删除关联数据,与之关联也删除
                                            - models.DO_NOTHING,删除关联数据,引发错误IntegrityError
                                            - models.PROTECT,删除关联数据,引发错误ProtectedError
                                            - models.SET_NULL,删除关联数据,与之关联的值设置为null(前提FK字段需要设置为可空)
                                            - models.SET_DEFAULT,删除关联数据,与之关联的值设置为默认值(前提FK字段需要设置默认值)
                                            - models.SET,删除关联数据,
                                                          a. 与之关联的值设置为指定值,设置:models.SET(值)
                                                          b. 与之关联的值设置为可执行对象的返回值,设置:models.SET(可执行对象)
    
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • zqbnqsdsmd 2019-12-29 23:09
    评论
    解决 无用
    打赏 举报
  • qq_975336710 2020-02-09 17:04

    owner = models.ForeignKey(User, verbose_name="作者")
    TypeError: __init_() missing 1 required positional argument: 'on_delete'
    先看报的错误,其他不用管,最后一行说明了问题,函数内部缺失了一个必要的参数'on_delete',那把这个参数加上去就好了,
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="作者")
    on_delete=models.CASCADE 意思是主表删除外键所在表也删除,看上面那楼解释就好了。
    外键还可以加 related_name='owner_user',啥都行,规范我忘了,反正可以通过这个使用_set直接去查询,具体可以百度。

    init方法使用与功能:
    1.用来构造初始化函数,用来给类的实例进行初始化属性,所以可以不需要返回值
    2.在创建类的实例时系统自动调用
    3.自定义类如果不定义的话,默认调用父类object的,同理继承也是,子类若无,调 用父类,若有,调用自己的

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题