【问题标题】:Djagno 3.0 with Python 3.6.12 1054 Errror following tutorial for Django 3.0Django 3.0 和 Python 3.6.12 1054 遵循 Django 3.0 教程的错误
【发布时间】:2021-01-27 14:34:22
【问题描述】:

大家好!

我想首先向这个社区介绍一下自己。我在 Python 方面有点介于初学者和中级之间,但在 Linux 和 MS System Admin 方面是高级的。因此,我熟悉 PHP、CSS、HTML、LAMP、XAMP、XMPP、DNS、虚拟化、云计算……所有……但是,关于编程,我从 Pasqual 开始,然后是 Visual Basic,然后有点Java 的我一直专注于 Python。

CPU:(对不起,我有点老 skul:i686(32 位) 内存:4 GB 操作系统:Ubuntu 操作系统 18.04 LTS

  • 不过,让我们继续讨论我的问题,我遇到了错误:

https://www.youtube.com/watch?v=6ManltU_8iU&t=9884s学习了一些教程之后

我正在关注:

(Django) root@borisdev:/home/Desktop/DjangoRelationships/MyProject# python manage.py shell
Python 3.6.12 (default, Aug 18 2020, 02:08:22) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from MyApp.models import Article, Reporter
>>> r = Reporter(first_name = 'John', last_name = 'Doe', email = 'abc@somemail.com')
>>> r.save()
>>> r1 = Reporter(first_name = 'Boris', last_name = 'Milanovic', email = 'milanvociful@gmail.com')
>>> r1.save()
>>> from datetime import date
>>> a = Article(headline = 'This is my first article', pub_date = date(2020, 10, 10), reporter=r)
>>> a.save()
Traceback (most recent call last):
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/home/Desktop/Django/lib/python3.6/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/home/Desktop/Django/lib/python3.6/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/home/Desktop/Django/lib/python3.6/site-packages/MySQLdb/connections.py", line 259, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'reporter_id' in 'field list'")

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

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/models/base.py", line 754, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/models/base.py", line 792, in save_base
    force_update, using, update_fields,
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/models/base.py", line 895, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/models/base.py", line 935, in _do_insert
    using=using, raw=raw,
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/models/query.py", line 1254, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
    cursor.execute(sql, params)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/home/Desktop/Django/lib/python3.6/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/home/Desktop/Django/lib/python3.6/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/home/Desktop/Django/lib/python3.6/site-packages/MySQLdb/connections.py", line 259, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1054, "Unknown column 'reporter_id' in 'field list'")
>>> 

如果需要任何其他资源,请告诉我到目前为止我最好的朋友“google”能够解决...

谢谢!

~鲍里斯

【问题讨论】:

  • 看起来像是丢失或未应用的迁移。
  • 我已经完成了 makemigrations 和 migrate....
  • 更新: (Django) root@borisdev:/home/Desktop/DjangoRelationships/MyProject# python manage.py makemigrations 未检测到更改 (Django) root@borisdev:/home /Desktop/DjangoRelationships/MyProject# python manage.py migrate 要执行的操作:应用所有迁移:MyApp、admin、auth、contenttypes、sessions 运行迁移:应用 MyApp.0002_auto_20201013_0704...Traceback(最近一次调用):文件“/ home/Desktop/Django/lib/python3.6/site-packages/django/db/backends/utils.py”,第 82 行,在 _execute 中返回 self.cursor.execute(sql)
  • 您是否在设置中激活了应用程序?

标签: python python-3.x linux pycharm virtualenv


【解决方案1】:

我也有同样的问题:

这是我的 settings.py:

"""
Django settings for RelationshipsInDjango project.

Generated by 'django-admin startproject' using Django 3.1.2.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '4umiwx4$3p6c2e7!x)+5kk0*s4py5e5n+ecn7=efskcer4@6v$@'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'MyApp'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'RelationshipsInDjango.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'RelationshipsInDjango.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydjango',
        'USER': 'debian-sys-maint',
        'PASSWORD': '6LJl7CAz9Lyuxxxx',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 2019-09-07
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    相关资源
    最近更新 更多