Python 如何使外部数据库查询不可编辑?

Python 如何使外部数据库查询不可编辑?,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,我有以下代码: 设置.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'tectcom', 'USER': 'test', 'PASSWORD': '***146***', 'HOST':

我有以下代码:

设置.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'tectcom',                     
        'USER': 'test',                    
        'PASSWORD': '***146***',                 
        'HOST': '',                     
        'PORT': '',                      
    },

    'cdr': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'ast',                     
        'USER': '123',                      
        'PASSWORD': '654',                 
        'HOST': '',                      
        'PORT': '',                     
    }
def cdr_user(request):
        cursor = connections['cdr'].cursor()
        calls = cursor.execute('SELECT * FROM cdr')
        return render_to_response("cdr_user.html",
                {'result':calls }, context_instance=RequestContext(request))
视图.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'tectcom',                     
        'USER': 'test',                    
        'PASSWORD': '***146***',                 
        'HOST': '',                     
        'PORT': '',                      
    },

    'cdr': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'ast',                     
        'USER': '123',                      
        'PASSWORD': '654',                 
        'HOST': '',                      
        'PORT': '',                     
    }
def cdr_user(request):
        cursor = connections['cdr'].cursor()
        calls = cursor.execute('SELECT * FROM cdr')
        return render_to_response("cdr_user.html",
                {'result':calls }, context_instance=RequestContext(request))
cdr\u user.html

{% for res in result %}

{{ res.billsec }}<br />

{% endfor %}
问题是我得到了一个异常值:“long”对象不可iterable“

/cdr\u用户处的类型错误/ “long”对象不可编辑 请求方法:获取 请求URL:http://localhost:8000/cdr_user/ Django版本:1.4.1 异常类型:TypeError 异常值: “long”对象不可编辑 异常位置:/usr/local/lib/python2.7/site-packages/django/template/defaulttags.py在渲染中,第144行 Python可执行文件:/usr/local/bin/Python Python版本:2.7.0 Python路径: ['/home/tectadmin/cdr/billing', “/usr/local/lib/python2.7/site packages/setuptools-0.6c11-py2.7.egg”, “/usr/local/lib/python2.7/site packages/pip-1.0-py2.7.egg”, “/usr/local/lib/python2.7/site packages/django_uncilled_pagination-1.1-py2.7.egg”, “/usr/local/lib/python27.zip”, “/usr/local/lib/python2.7”, “/usr/local/lib/python2.7/plat-linux2”, “/usr/local/lib/python2.7/lib-tk”, “/usr/local/lib/python2.7/lib old”, “/usr/local/lib/python2.7/lib dynload”, '/usr/local/lib/python2.7/site packages'] 服务器时间:Sab,1台2012 19:56:10-0300 模板呈现期间出错 在template/home/tectadmin/cdr/billing/config/templates/cdr_user.html中,第21行出现错误 “long”对象不可编辑 11文本缩进:6em; 12 } 13 14{%extends“index_cliente.html”%} 15{%负载无限%} 16{%block title%}CDR{%endblock%} 17{%块内容%} 18 19 20 21{res在结果%中的百分比} 22 23{{res.billsec}}
24 25{%endfor%} 26 27 28 29 30
31 回溯切换到复制和粘贴视图 /get_响应中的usr/local/lib/python2.7/site-packages/django/core/handlers/base.py 响应=回调(请求,*回调参数,**回调参数)。。。 ▶ 局部变量 /cdr_用户中的home/tectadmin/cdr/billing/config/views.py {'result':result},context_instance=RequestContext(request))。。。 ▶ 局部变量 如何使结果在模板中显示? 我已经看过了一些文档和其他文档,但我仍然迷恋于代码


谢谢。

要在Python中迭代SQL查询的结果,请使用
cursor.fetchall()
将其转换为列表列表。有一个非常方便的方法可以将这些结果转化为您可以轻松访问的对象:

class SQLRow(object):
    def __init__(self, cursor, row):
        for (attr, val) in zip((d[0] for d in cursor.description), row) :
            setattr(self, attr, val)
一旦你有了这个类,这很简单:

def cdr_user(request):
    cursor = connections['cdr'].cursor()
    calls = cursor.execute('SELECT * FROM cdr')
    result = [SQLRow(cursor, r) for r in cursor.fetchall()]
    return render_to_response("cdr_user.html",
            {'result': result }, context_instance=RequestContext(request))
这样,
billsec
属性(以及所有其他属性)仍然可以在模板中访问。

cursor.execute()
不会返回iterable。它会在位修改光标对象。这里有一些关于这个的

您需要调用
.fetchone()
.fetchmany()或
.fetchall()
来检索结果,结果应该是可编辑的,例如:

def cdr_user(request):
  cursor = connections['cdr'].cursor()
  cursor.execute('SELECT * FROM cdr')
  calls = cursor.fetchall()
  return render_to_response("cdr_user.html",
                            {'result':calls }, 
                            context_instance=RequestContext(request))

您能否发布异常值的完整回溯:'long'对象不可编辑错误?好的,我粘贴了所有异常。为什么在Django应用程序中运行原始SQL(而不是使用其ORM存储对象?)?表cdr不是我项目的模型,它是另一个非Django项目的一部分。在这种情况下,我可以使用ORM吗?不,不过我知道你需要做什么。我会写一个答案。这不适用于他现有的模板(无法通过
res.billsec
访问)。它工作得很好,非常感谢!!如果你需要关于电话的任何信息,请告诉我。