2014-05-19 46 views
0

這裏是代碼:Django的NameError當試圖訪問數據

from django.db import models 

import datetime 
from django.utils import timezone 

class data_model(models.Field): 
    description = "return and create data objects for visulaizations" 

    def __init__(self, days, action): 
     self.days = days 
     self.action = action 

     if(self.action == ""): 
      self.action = "inspections" 

     getVioPoints(self.action, self.days) 
    #end init 

    def getVioPoints(self): 
     #get points 
     if(self.action == "violations"): 
      apendQuery = "where osha_violation_indicator is true" 
     elif(self.action == "inspections"): 
      apendQuery = "where osha_violation_indicator is false" 
     else: 
      apendQuery = "" 

     from django.db import connections   
     conn = connections['opengov_db'].cursor() 

     conn.execute(""" 
      select distinct a.estab_name, b.latitude, b.longitude, a.site_address, a.site_city, a.site_state, a.site_zip 
      from osha_inspection a 
      join latitude_longitude_lookup b on cast(a.activity_nr as text)= b.source_data_id 
      """,apendQuery,""" 
      and close_case_date >= now() - interval """,self.days,""" days' 
      and b.latitude is not null; """) 

     row = conn.fetchone() 
     print row 

我收到此錯誤:

Unhandled exception in thread started by <function wrapper at 0x9d9656c> 
Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 93, in wrapper 
    fn(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 101, in inner_run 
    self.validate(display_num_errors=True) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 310, in validate 
    num_errors = get_validation_errors(s, app) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 34, in get_validation_errors 
    for (app_name, error) in get_app_errors().items(): 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 196, in get_app_errors 
    self._populate() 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 75, in _populate 
    self.load_app(app_name, True) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 99, in load_app 
    models = import_module('%s.models' % app_name) 
    File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module 
    __import__(name) 
    File "/var/www/opengov/mapPrototypePY/opengov/map/models.py", line 6, in <module> 
    class data_model(models.Field): 
    File "/var/www/opengov/mapPrototypePY/opengov/map/models.py", line 39, in data_model 
    row = conn.fetchone() 
NameError: name 'conn' is not defined 

而且我不知道爲什麼。我知道該查詢正在工作。但是,這似乎忽略了包含row = conn.fetchone()的行上的連接。我搜索了一下,沒有發現任何提示。有任何想法嗎? 謝謝

+1

請發佈完整的回溯。另外,這是你正在運行的相同的代碼? (什麼是縮進?)謝謝。 – alecxe

+0

@alecxe根據要求。 –

+1

您需要發佈*完整*代碼。您的回溯與您發佈的代碼段不匹配:特別是,似乎有某種類型的定義('models.Field'),這應該是其中的一部分。我懷疑有一個縮進錯誤,但是沒有看到整個代碼,這是不可能告訴的。 –

回答

2

正如評論中所述,這是一個縮進錯誤。

您在運行時沒有得到實際的IndentationError的原因是因爲代碼仍然在語法上有效。可能發生的情況是代碼在類級別被解釋爲與方法定義處於同一級別:完全可能擁有該級別的代碼,例如設置類級變量和編譯器(IndentationError是一個編譯時間例外)不知道conn不在此範圍內。

+0

謝謝@Daniel Roseman。如果你認爲這個問題是相關的,請給它舊的讚許:)。 –