个人报错TypeError: ‘type‘ object is not iterable

时间:2025-03-31 18:19:24

django报错 ‘type’ object is not iterable,个人报错原因解析

E:\django_project\my_blog\article\ changed, reloading.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "E:\Python\Python37\lib\", line 926, in _bootstrap_inner
    ()
  File "E:\Python\Python37\lib\", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "E:\django_project\my_blog\venv\lib\site-packages\django\utils\", line 54, in wrapper
    fn(*args, **kwargs)
  File "E:\django_project\my_blog\venv\lib\site-packages\django\core\management\commands\", line 109, in inner_run
    autoreload.raise_last_exception()
  File "E:\django_project\my_blog\venv\lib\site-packages\django\utils\", line 77, in raise_last_exception
    raise _exception[1]
  File "E:\django_project\my_blog\venv\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors()()
  File "E:\django_project\my_blog\venv\lib\site-packages\django\utils\", line 54, in wrapper
    fn(*args, **kwargs)
  File "E:\django_project\my_blog\venv\lib\site-packages\django\__init__.py", line 24, in setup
    (settings.INSTALLED_APPS)
  File "E:\django_project\my_blog\venv\lib\site-packages\django\apps\", line 122, in populate
    app_config.ready()
  File "E:\django_project\my_blog\venv\lib\site-packages\django\contrib\admin\", line 24, in ready
    ()
  File "E:\django_project\my_blog\venv\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "E:\django_project\my_blog\venv\lib\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "E:\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "E:\django_project\my_blog\article\", line 7, in <module>
    (ArticlePost)
  File "E:\django_project\my_blog\venv\lib\site-packages\django\contrib\admin\", line 102, in register
    for model in model_or_iterable:
TypeError: 'type' object is not iterable

网上查找很多,但是都对不上,有的竟然要改源码。然后仔细过了一遍报错前写过的代码,找到错误原因:
定义models的类时,忘记添加继承了

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


# 博客文章数据模型
from django.utils import timezone

# ↓↓↓↓↓↓↓↓↓↓报错原因↓↓↓↓↓↓↓
class ArticlePost():
# ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
    # 文章作者, on_delete数据删除方式,此处是级联删除
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    # 文章标题,max_length设置最大长度,并且不能为空
    title = models.CharField(max_length=100)

    # 文章正文内容
    body = models.TextField()

    # 文章创建时间,默认未创建数据的时间
    created = models.DateTimeField(default=timezone.now)

    # 文章更新时间,数据更新时,自动写入当前系统时间
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        # ordering 指定模型返回数据的排列顺序
        # -created 表明数据倒叙排列
        ordering = ("-created",)

    # 函数 __str__(self)定义调用对象的 str() 方法时,返回值的内容
    def __str__(self):
        # 将文章的标题返回
        return self.title


修改后,class ArticlePost():

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


# 博客文章数据模型
from django.utils import timezone

# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
class ArticlePost(models.Model):
    # 文章作者, on_delete数据删除方式,此处是级联删除
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    # 文章标题,max_length设置最大长度,并且不能为空
    title = models.CharField(max_length=100)

    # 文章正文内容
    body = models.TextField()

    # 文章创建时间,默认未创建数据的时间
    created = models.DateTimeField(default=timezone.now)

    # 文章更新时间,数据更新时,自动写入当前系统时间
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        # ordering 指定模型返回数据的排列顺序
        # -created 表明数据倒叙排列
        ordering = ("-created",)

    # 函数 __str__(self)定义调用对象的 str() 方法时,返回值的内容
    def __str__(self):
        # 将文章的标题返回
        return self.title