参照文档,在具体的应用目录下找到models.py,增加需要的表:
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice
然后上一层目录找到settings.py,修改DATABASE一段的配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'post 'NAME': 'db_test', # Or path to database file if usi
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. No
'PORT': '', # Set to empty string for default. Not
}
}
在manage.py所在目录,执行python manage.py syncdb,这样会自动在db_test中生成对应的数据表。
django版本:1.2.1
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice
然后上一层目录找到settings.py,修改DATABASE一段的配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'post 'NAME': 'db_test', # Or path to database file if usi
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. No
'PORT': '', # Set to empty string for default. Not
}
}
在manage.py所在目录,执行python manage.py syncdb,这样会自动在db_test中生成对应的数据表。
django版本:1.2.1
转载于:https://blog.51cto.com/co63oc/325596