讓我指出,這是第2周與Django - 這意味着我是一個新手。目標:要使用自定義模式,從Postges數據庫檢索數據並在Django的自定義Django模型,返回數據查看
settings.py
這些數據返回到視圖,然後到模板DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'opengov_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'OGSDB',
'USER': 'web_user',
'PASSWORD': 'admin123',
'HOST': '10.187.240.117',
'PORT': '5432',
}
}
而且這裏是在模型中。潘岳:
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"
print self.action
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; """)
for row in cursor.fetchall():
print row['estab_name']
return row
最後,這裏是views.py:
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
def index(request):
# Obtain the context from the HTTP request.
context = RequestContext(request)
return render_to_response('map/index.html', {'title':'Home Page'}, context)
#end index
問題: models.py中的查詢將返回一組與業務關聯的座標點和元數據。我想遍歷它,並返回視圖中的數據,以便我可以將它傳遞給模板文件。需要展開views.py文件才能從models.py中提取數據,但這是如何完成的?
我環顧四周,我知道這不是返回DB對象的典型Django方法。但是,必須有一種方法可以將記錄集返回到視圖,而無需使用Django教程使用SQLite 3提供的內容。我真的可以在這裏使用一些指導。我沒有發現任何在線描述如何以這種方式做到這一點。這是否意味着你不能?
僅供參考:在瀏覽器中加載index.html文件時不返回任何錯誤。不知道如何驗證數據是否被檢索。我曾嘗試:蟒蛇models.py這是返回什麼 -
Traceback (most recent call last):
File "models.py", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/dist-packages/django/db/models/__init__.py", line 5, in <module>
from django.db.models.query import Q
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 17, in <module>
from django.db.models.deletion import Collector
File "/usr/local/lib/python2.7/dist-packages/django/db/models/deletion.py", line 4, in <module>
from django.db.models import signals, sql
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/__init__.py", line 4, in <module>
from django.db.models.sql.subqueries import *
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/subqueries.py", line 12, in <module>
from django.db.models.sql.query import Query
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 22, in <module>
from django.db.models.sql import aggregates as base_aggregates_module
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/aggregates.py", line 9, in <module>
ordinal_aggregate_field = IntegerField()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 116, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
任何援助將最受大家歡迎。謝謝。
我是新到Django,所以使模型成爲一個類似乎是基於我在這裏找到的教程模型的正常慣例的一部分:https://docs.djangoproject.com/en/1.6/intro/tutorial01/。如何使用ORM與Postgres? –
但正如我所說,你不會在任何情況下繼承Field。在ORM中使用sqlite與Postgres沒有什麼區別,這就是要點:它將數據庫後端之間的差異抽象出來。 –
我很欣賞你的答案,但你可以編輯你的答案,並添加一個簡單的代碼示例或提供一個鏈接,顯示如何做到這一點?謝謝。 –