sqlite3.OperationalError:无此类表格:django内容类型

rm5edbpk  于 2022-12-20  发布在  Go
关注(0)|答案(5)|浏览(146)

我正在尝试运行不是由我的应用编写的应用。当我编写python manage.py makemigrations时,我得到:

Traceback (most recent call last):
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
        return self.cursor.execute(sql, params)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
        return Database.Cursor.execute(self, query, params)
    sqlite3.OperationalError: no such table: django_content_type

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "manage.py", line 22, in <module>
        execute_from_command_line(sys.argv)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
        utility.execute()
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init__.py", line 341, in execute
        django.setup()
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 27, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 115, in populate
        app_config.ready()
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\apps.py", line 23, in ready
        self.module.autodiscover()
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
        autodiscover_modules('admin', register_to=site)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\module_loading.py", line 50, in autodiscover_modules
        import_module('%s.%s' % (app_config.name, module_to_search))
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 978, in _gcd_import
      File "<frozen importlib._bootstrap>", line 961, in _find_and_load
      File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 678, in exec_module
      File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
      File "C:\Users\direwolf\Documents\web\python\alexbog80-motivity-3e5c21f03b3e\app\motivity\admin.py", line 23, in <module>
        admin.site.register(Offer, OfferAdmin)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\sites.py", line 110, in register
        system_check_errors.extend(admin_obj.check())
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\options.py", line 117, in check
        return self.checks_class().check(self, **kwargs)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 520, in check
        errors.extend(self._check_list_display(admin_obj))
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 596, in _check_list_display
        for index, item in enumerate(obj.list_display)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 596, in <listcomp>
        for index, item in enumerate(obj.list_display)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 604, in _check_list_display_item
        elif hasattr(model, item):
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tagging\fields.py", line 55, in __get__
        return edit_string_for_tags(Tag.objects.usage_for_model(owner))
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tagging\models.py", line 157, in usage_for_model
        usage = self.usage_for_queryset(queryset, counts, min_count)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tagging\models.py", line 183, in usage_for_queryset
        extra_joins, extra_criteria, params)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tagging\models.py", line 113, in _get_usage
        'content_type_id': ContentType.objects.get_for_model(model).pk,
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\contenttypes\models.py", line 52, in get_for_model
        ct = self.get(app_label=opts.app_label, model=opts.model_name)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 379, in get
        num = len(clone)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 238, in __len__
        self._fetch_all()
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 1087, in _fetch_all
        self._result_cache = list(self.iterator())
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 54, in __iter__
        results = compiler.execute_sql()
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py", line 835, in execute_sql
        cursor.execute(sql, params)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py", line 79, in execute
        return super(CursorDebugWrapper, self).execute(sql, params)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
        return self.cursor.execute(sql, params)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\utils.py", line 94, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
        return self.cursor.execute(sql, params)
      File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
        return Database.Cursor.execute(self, query, params)
    django.db.utils.OperationalError: no such table: django_content_type

我该怎么办?
更新1:python manage.py migrate追溯:

Traceback (most recent call last):
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: django_content_type

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init__.py", line 341, in execute
    django.setup()
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\apps\registry.py", line 115, in populate
    app_config.ready()
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\apps.py", line 23, in ready
    self.module.autodiscover()
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\Users\direwolf\Documents\web\python\alexbog80-motivity-3e5c21f03b3e\app\motivity\admin.py", line 23, in <module>
    admin.site.register(Offer, OfferAdmin)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\sites.py", line 110, in register
    system_check_errors.extend(admin_obj.check())
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\options.py", line 117, in check
    return self.checks_class().check(self, **kwargs)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 520, in check
    errors.extend(self._check_list_display(admin_obj))
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 596, in _check_list_display
    for index, item in enumerate(obj.list_display)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 596, in <listcomp>
    for index, item in enumerate(obj.list_display)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 604, in _check_list_display_item
    elif hasattr(model, item):
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tagging\fields.py", line 55, in __get__
    return edit_string_for_tags(Tag.objects.usage_for_model(owner))
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tagging\models.py", line 157, in usage_for_model
    usage = self.usage_for_queryset(queryset, counts, min_count)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tagging\models.py", line 183, in usage_for_queryset
    extra_joins, extra_criteria, params)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tagging\models.py", line 113, in _get_usage
    'content_type_id': ContentType.objects.get_for_model(model).pk,
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\contenttypes\models.py", line 52, in get_for_model
    ct = self.get(app_label=opts.app_label, model=opts.model_name)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 379, in get
    num = len(clone)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 238, in __len__
    self._fetch_all()
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 54, in __iter__
    results = compiler.execute_sql()
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py", line 835, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\direwolf\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils

.操作错误:无此类表格:django内容类型
更新2:models.py中的产品类别

class Offer(TimeStampMixin, SEOFieldsMixin):
    title = models.CharField(u'Название предложения', max_length=255, blank=False)
    discription = models.TextField(u'Красивое описание оффера', null=False, blank=False)
    cover = models.ImageField(u'Обложка', blank=False, null=False)
    tags = TagAutocompleteField(blank=False, verbose_name='Теги')
    active = models.BooleanField(u'Активный?', default=True)
    publish = models.BooleanField(u'Показывать на сайте?', default=True)

    def preview_image(self):
        # try:
        thumbnail = get_thumbnail(self.cover, 'x50', crop='center')
        # except TypeError:
        #     return u'Нет картинки'
        # except:
        #     return u'Нет картинки'  # if original img not exist
        return '<a href="%s/"><img src="%s"/></a>' % (self.id, thumbnail.url)

    preview_image.short_description = u'Обложка'
    preview_image.allow_tags = True

    class Meta:
        verbose_name = u'Предложение'
        verbose_name_plural = u'Предложения'
        ordering  = ('-modified_value',)
        get_latest_by = 'created_value'

    def __unicode__(self):
        return u'%s' % self.title

我没有找到管理类只有这个:

from django.contrib import admin

from app.motivity.models import TaskOffer, Offer, UserOffers

class TaskOfferAdmin(admin.TabularInline):
    model = TaskOffer
    exclude = ['meta_title', 'meta_description', 'meta_keywords']

class OfferAdmin(admin.ModelAdmin):
    list_display = ['preview_image','title','tags', 'publish']
    list_display_links = ['title']
    list_filter = ['tags']
    inlines = [TaskOfferAdmin, ]
    fields = ['title', 'discription', 'cover', 'tags']

class UserOfferAdmin(admin.ModelAdmin):
    pass

admin.site.register(UserOffers, UserOfferAdmin)
admin.site.register(Offer, OfferAdmin)
qltillow

qltillow1#

我在Mac OS 10.15.2上使用Python 3.6.5Django == 2.1.7时遇到了同样的问题。我通过手动创建包含以下列的表django_content_type修复了这个问题:id, app_label, model
在运行python manage.py migrate时出现错误,应该在settings.py文件中的DATABASE键上指定的路径下创建了*.sqlite3文件。

    • 示例:**
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'base_site.sqlite3'),
    }
}

我的Squlite DB文件名是base_site.sqlite3,在项目的BASE_DIR中创建。

    • 应遵循的步骤:**
  • 使用以下命令连接到创建的SQLite DB:
sqlite3 <DB filename>
  • 使用以下命令创建表:
CREATE TABLE IF
NOT EXISTS "django_content_type" (
 "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
 "app_label" varchar(100) NOT NULL,
 "model" varchar(100) NOT NULL
);
  • 最后,使用以下命令运行迁移:
python manage.py migrate --fake-initial

这应按预期运行迁移。

ekqde3dh

ekqde3dh2#

如果您的代码在导入时而不是运行时使用contenttypes数据库信息,则会出现此问题。例如,如果您添加了在模块级别或类级别运行的代码,如下所示:

TERM_CONTENT_TYPE = ContentType.objects.get_for_model(Term)

但您只是在运行创建该内容类型表的初始迁移 * 之后 * 才添加此错误,则下一个在新数据库上运行迁移的人将收到此错误。
在这种情况下,解决方案是将这样的代码移到只有在调用方法时才运行的地方,比如视图或其他方法。

qv7cva1a

qv7cva1a3#

只需删除您的db.sqlite3文件....并再次运行命令---〉py manage.py migrate然后,py manage.py makemigrations应用程序名称最后,py manage.py migrate...按照此过程希望您将修复您的prblm

pengsaosao

pengsaosao4#

对于测试数据库,简单修复可以是:

# Remove database and the history  cache for of applied migrations
rm db.sqlite3
find . | grep "__pycache__" | xargs sudo rm -rf

# Check that migration are not applied
python manage.py showmigrations

# Then apply fisrt only the admin module migrations
python manage.py migrate admin

# Then apply all others
python manage.py migrate

我认为这应该是一个很好的做法,总是应用管理迁移在第一次,然后其他人以后。

bihw5rsg

bihw5rsg5#

看起来我用的是python 3x而这个应用程序是用python 2x写的

相关问题