0
我的python知識仍然有限,我不知道如何在django中執行這個函數和類。 後來,而不是打印我會做四個查詢循環到原始數據庫,有人可以幫助我做到這一點正確的方式嗎?for循環功能
class WeeklyReports(models.Model):
def FourWeeks(self, year, week):
self.year = year
self.week = week
end = datetime(year, 1, 1) + relativedelta(weeks=week-1, weekday=SU)
start = end - relativedelta(weeks=4, weekday=MO)
mint, maxt = datetime.min.time(), datetime.max.time()
for dt in rrule(WEEKLY, start, count=4):
yield dt.combine(dt, mint), dt.combine(dt + timedelta(days=6), maxt)
for start, end in FourWeeks(year, week):
print start, end
我有望獲得類似的東西就如這裏countdown 4 weeks (week by week) back with python/django
但我得到這個:
>>> from weeklyreport.models import WeeklyReports
>>> w = WeeklyReports()
>>> w.FourWeeks(2013, 22)
<generator object FourWeeks at 0x1eb5050>
>>> w.FourWeeks(2013, 22)
<generator object FourWeeks at 0x1eb50a0>
>>> w.FourWeeks(2013, 22)
<generator object FourWeeks at 0x1eb5050>
>>> w.FourWeeks(2013, 22)
<generator object FourWeeks at 0x1eb50a0>
此查詢我將與打印後替換:
cursor = connections['nocdb'].cursor()
cursor.execute("SELECT DISTINCT (p.name) AS platform, count(e.id) AS count FROM event e, lu_platform p WHERE e.platform_id = p.id AND e.sourcetype_id = 1 AND e.event_datetime BETWEEN %s AND %s AND e.sender_id NOT IN (759, 73) GROUP BY p.name ORDER BY p.name", [start, end])
但現在我無法啓動運行Django shell 報告$蟒蛇manage.py殼 /weeklyreport/models.py」 61行,在 爲起點,終點在FourWeeks(年,周): NameError:名字'FourWeeks'沒有被定義 –
Robert
@ user2859660對不起,現在已經修復了,我認爲打印語句應該放在你的視圖中,或者可以在shell窗口中執行。 –
謝謝你的幫助,我發佈了這個問題,因爲我在那裏在我看來,我有工作定義 – Robert