1

更新关闭 - 我想我经历了它,这对我来说有点愚蠢。

python manage.py 检查数据库>模型.py

我忘记将输出通过管道传输到文件!

我已经将它转储到我的 app/models.py 中,现在我只是想让它从那里运行但更进一步。

我试图让 django 在 SQL Server 2008 R2 服务器上运行。

我已经弄清楚了 FreeTDS/unixODBC 连接。我可以成功地对数据库进行 isql 和 tsql 查询。

我已经能够像这样直接在python中进行连接 -

import pyodbc
cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER=servername;DATABASE=dbname;UID=user;PWD=pass;TDS_Version=8.0;')
cursor = cnxn.cursor()
result = cursor.execute("select name from tablea where name = 'hello'")
print result

如果我在 django shell 中做一个 inspectdb 就可以了。它返回 70 多个表格,并且它们的布局非常好。

一旦我尝试做一个 syndb,它就会溢出以下错误 -

Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 112, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/sql.py", line 216, in emit_post_sync_signal
    interactive=interactive, db=db)
  File "/usr/local/lib/python2.6/dist-packages/django/dispatch/dispatcher.py", line 185, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/management/__init__.py", line 82, in create_permissions
    ctype = ContentType.objects.db_manager(db).get_for_model(klass)
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/contenttypes/models.py", line 47, in get_for_model
    defaults = {'name': smart_text(opts.verbose_name_raw)},
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 154, in get_or_create
    return self.get_queryset().get_or_create(**kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 373, in get_or_create
    return self.get(**lookup), False
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 301, in get
    num = len(clone)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 77, in __len__
    self._fetch_all()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 710, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 771, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 48, in as_sql
    self._get_ordering(out_cols, supports_offset_clause or not do_offset)
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 154, in _get_ordering
    ordering, ordering_group_by = self.get_ordering()
ValueError: too many values to unpack

同样,如果我尝试像这样在 django 中进行查询-

from myapp.models import tablea
tablea.objects.filter(id=5)

它失败并显示以下错误消息 -

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 71, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 710, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 771, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 48, in as_sql
    self._get_ordering(out_cols, supports_offset_clause or not do_offset)
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 154, in _get_ordering
    ordering, ordering_group_by = self.get_ordering()
ValueError: too many values to unpack

我不相信这些错误与此处找到的其他线程有关-

ValueError:解包 Django 的值太多

但我不是专家,所以我可能是错的。我知道我确实尝试更改我的密码,但它不会让我连接类似的错误消息 -

>>> u = User.objects.get(username="user")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 301, in get
    num = len(clone)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 77, in __len__
    self._fetch_all()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 710, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 771, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 48, in as_sql
    self._get_ordering(out_cols, supports_offset_clause or not do_offset)
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 154, in _get_ordering
    ordering, ordering_group_by = self.get_ordering()
ValueError: too many values to unpack

编辑添加 - 进行查询时,我的模型定义如下 -

from django.db import models

# Create your models here.

class tablea(models.Model):
    tablea_id = models.IntegerField()
    tablea_name = models.CharField(max_length=32)
    tablea_duration = models.IntegerField()
class Meta:
    db_table = u'tablea'
    def __unicode__(self):
        return self.tablea_name, tablea_duration

编辑以添加我已经完成了全新的重新安装。擦除虚拟机并使其恢复活力。这是完成的步骤的细分

apt-get install gcc g++ make python-dev

下载 unixODBC

tar xzvf unixODBC-2.3.2.tar.gz

。/配置

制作

安装

下载 FreeTDS

tar xzvf freetds-stable.tgz

./configure --with-tdsver=8.0 --with-unixodbc=usr/local

制作

进行安装

ldconfig -v

odbc.ini

[mssql]
Description             = MSSQL Server
Driver                  = freetds
Database                = database
Server              = server
Port            = 1433
TDS_Version             = 8.0

odbcinst.ini

[freetds]
Description     = MS SQL database access with Free TDS
Driver          = /usr/local/lib/libtdsodbc.so
Setup           = /usr/local/lib/libtdsS.so
UsageCount      = 1

freetds.conf

[mssql]
        host = server
        port = 1433
        tds version = 8.0

tsql -S mssql -U 用户 -P 密码有效

isql -v mssql 用户密码有效(由于某种原因,简单的 isql -v mssql 无效)

apt-get 安装 python-setuptools

easy_install pip

点安装pyodbc

点安装 Django==1.6

pip install django-pyodbc-azure

设置.py

DATABASES = {
   'default': {
       'ENGINE': 'sql_server.pyodbc',
       'HOST': "server\instance",
       'USER': "username",
       'PASSWORD': "password",
       'NAME': "database",
       'OPTIONS': {
           'host_is_server': True,
           'driver': 'FreeTDS',
           'dsn': 'MSSQL',
       },
   }
}
4

1 回答 1

3

是版本不兼容的问题。

Django 1.6 改变了django.db.models.sql.compiler.SQLCompiler.get_ordering. 它返回一个3 元素的元组,但pyodbc只解包两个元素。

如果您查看Django 1.5 版本,您会发现它曾经像 pyodbc 期望的那样返回一个2 元素元组。

于 2013-11-12T17:49:56.810 回答