Python Tutorial 第四堂(2)建立 App 與基本 ORM
|
Python Tutorial 第四堂(1)Django 起步走 << 前情 你已經建立第一個 Django 專案,那麼專案中有哪些東西呢?我們來看看 … 相關的組態設定該如何進行,我們還是實際從練習中瞭解 … 練習 10:建立資料庫與 App 開啟 mysite/settings.py,將其中 ...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/home/caterpillar/scripts/venv/mysite/db.sqlite3',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
...
接著執行 接下來鍵入指令 from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def __unicode__(self):
return self.question
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice_text
這建立了兩個資料模型 這個 app 剛建立,你必須讓目前專案知道,這要在 mysite/settings.py 中設定,找到其中的 INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'polls'
)
接著你可以分別執行 接著你可以鍵入 或者是建立 對 接下來,是該進行頁面設計了,這在下篇再來談了 … |

Java 學習之路










Jie Li
07/21Hi, you need to import timezone from django.utils and datetime in polls/models.py
throlicon
09/05from django.db import models
from django.utils import timezone
from datetime import datetime, timedelta
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - timedelta(days=1)