DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'djangodb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}
修改完后进入DOS进入项目目录下执行python manage.py shell命令启动交互界面输入一下代码验证数据库配置是否成功。没报错则成功!
#vim: set fileencoding=utf-8 :
from django.db import models
# Create your models here.
class Company(models.Model):
full_name = models.CharField(u'公司全称' ,max_length=30)
address = models.CharField(u'地址', max_length=50)
tel = models.CharField(u'电话', max_length=15,blank=True)
def __unicode__(self):
return '%s %s %s' % (self.full_name,self.address,self.tel)
class Product(models.Model):
product_name = models.CharField(u'产品名称', max_length=30)
price = models.FloatField(u'价格')
stock = models.IntegerField(u'库存', max_length=5)
company = models.ForeignKey(Company)
create_date = models.DateField(u'生产日期')
def __unicode__(self):
return self.product_name
#设置一些与特定模型相关的选项
class Meta:
ordering = ['create_date']
5,模型安装(修改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',
'DjangoMysqlSite.products',
)