繁体   English   中英

Django 继续在 mysql 查询中为表名加上引号,查询将无法正常工作

[英]Django keeps on putting quotes around table name in mysql query and the query wont work

MySql 中的表名是用户凭据,并且 django 生成的查询在表名周围没有引号的情况下工作。 model 是使用 django 中的 inspectdb 命令创建的,我试图将 django 管理面板作为数据库管理器。

模型.py:

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey has `on_delete` set to the desired behavior.
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models


class Usercreds(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=20, blank=True, null=True)
    password = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'usercreds'

设置.py:

"""
Django settings for admindb project.

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

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

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

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(4n(txlca35l2nk$(#@^p9iin_a5m#&lvu7w=!-8tbb^7e0+33'

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

ALLOWED_HOSTS = []


# Application definition

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

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 = 'admindb.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 = 'admindb.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'testdb',
        'USER': 'postgres',
        'PASSWORD': 'wtfigo!!',
        'HOST': 'localhost',
        'PORT': '5432',
    },
    'mysql': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'init_command': 'SET default_storage_engine=INNODB',
            'database': 'testdb',
            'user' : 'root',
            'password' : 'Ftpol018@',
        },
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.2/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/2.2/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/2.2/howto/static-files/

STATIC_URL = '/static/'

我得到的错误是:

ProgrammingError at /admin/dbmanagemysql/usercreds/
relation "usercreds" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "usercreds"

来自 django 的追溯:

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/dbmanagemysql/usercreds/

Django Version: 2.2.11
Python Version: 3.6.9
Installed Applications:
['dbmanagepsql',
 'dbmanagemysql',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed 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']



Traceback:

File "/home/alex/.local/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
  84.                 return self.cursor.execute(sql, params)

The above exception (relation "usercreds" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "usercreds"
                                          ^
) was the direct cause of the following exception:

File "/home/alex/.local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/alex/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/alex/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/alex/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
  606.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/home/alex/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/alex/.local/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/home/alex/.local/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "/home/alex/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/home/alex/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/alex/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in changelist_view
  1685.             cl = self.get_changelist_instance(request)

File "/home/alex/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in get_changelist_instance
  744.             sortable_by,

File "/home/alex/.local/lib/python3.6/site-packages/django/contrib/admin/views/main.py" in __init__
  82.         self.get_results(request)

File "/home/alex/.local/lib/python3.6/site-packages/django/contrib/admin/views/main.py" in get_results
  210.         result_count = paginator.count

File "/home/alex/.local/lib/python3.6/site-packages/django/utils/functional.py" in __get__
  80.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/alex/.local/lib/python3.6/site-packages/django/core/paginator.py" in count
  91.             return c()

File "/home/alex/.local/lib/python3.6/site-packages/django/db/models/query.py" in count
  392.         return self.query.get_count(using=self.db)

File "/home/alex/.local/lib/python3.6/site-packages/django/db/models/sql/query.py" in get_count
  504.         number = obj.get_aggregation(using, ['__count'])['__count']

File "/home/alex/.local/lib/python3.6/site-packages/django/db/models/sql/query.py" in get_aggregation
  489.         result = compiler.execute_sql(SINGLE)

File "/home/alex/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1140.             cursor.execute(sql, params)

File "/home/alex/.local/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
  99.             return super().execute(sql, params)

File "/home/alex/.local/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
  67.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "/home/alex/.local/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute_with_wrappers
  76.         return executor(sql, params, many, context)

File "/home/alex/.local/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
  84.                 return self.cursor.execute(sql, params)

File "/home/alex/.local/lib/python3.6/site-packages/django/db/utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "/home/alex/.local/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
  84.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /admin/dbmanagemysql/usercreds/
Exception Value: relation "usercreds" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "usercreds"
                                          ^

如果有办法删除引号或不生成带有引号的查询将解决问题。 我曾尝试在 models.py 中重命名表名,但似乎没有任何效果。 如果你们熟悉这个或以某种方式使用 django 管理面板作为数据库管理器 ui 在任何数据库上执行 CRUD 操作,请帮助我。

问题不在于 Django 在您的表名周围加上引号,而是您正在为该表查询错误的数据库。 在您的情况下,它正在查询您的 postgres 数据库,因为这是您设置为default的内容,这是 Django 在您没有指定要使用的任何其他特定数据库时使用的数据库别名。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'testdb',
        'USER': 'postgres',
        'PASSWORD': 'wtfigo!!',
        'HOST': 'localhost',
        'PORT': '5432',
    },
    'mysql': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'init_command': 'SET default_storage_engine=INNODB',
            'database': 'testdb',
            'user' : 'root',
            'password' : 'Ftpol018@',
        },
    }
}

为了证明是这种情况,您可以使用.using('db_alias')来 select 使用正确的数据库

all_usercreds = Usercreds.objects.using('mysql').all()

如果您不想在要引用 model 时始终调用.using ,则可以定义自定义数据库路由器并将其注册到settings.DATABASE_ROUTERS中,如下所示: my_app.db_routers.py

from my_app.models import Usercreds
class CustomDatabaseRouter(object):
    def db_for_read(self, model, **hints):        
        if model == Usercreds:
            return 'mysql'
        return None

    def db_for_write(self, model, **hints):
        if model == Usercreds:
            return 'mysql'
        return None

app/settings.py

DATABASE_ROUTERS = ('my_app.db_routers.CustomDatabaseRouter',)

来源: https://docs.djangoproject.com/en/3.0/topics/db/multi-db/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM