<span style="font-family: Arial, Helvetica, sans-serif; background-color: #ffffff;"> django数据库建表不用写MYSQL语句,直接在models.py中定义好model类,然后执行 python manage.py syncdb 就可以直接自动创建对应的表了。如下代码所示</span>
class Publisher(models.Model): name = models.CharField(max_length = 30) address = models.CharField(max_length = 50) city = models.CharField(max_length = 60) state_province = models.CharField(max_length = 60) country = models.CharField(max_length = 50) website = models.URLField() class Author(models.Model): first_name = models.CharField(max_length = 30) last_name = models.CharField(max_length = 40) email = models.EmailField() class Book(models.Model): title = models.CharField(max_length = 50) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publish_date = models.DateField() |
就可以建立对应的四个MSQ表格,这种做法本地操作没问题,可以DJANGO项目部署到SAE上面如何实现呢?
1. 本地得安装 SAE 环境
2. settings.py 中加入以下代码
if True: from sae._restful_mysql import monkey monkey.patch() MYSQL_HOST_M = 'w.rdc.sae.sina.com.cn' MYSQL_PORT = '3307' MYSQL_USER = 'APP_ACCESS' MYSQL_PASS = 'APP_SECRET' MYSQL_DB = 'app_APP_NAME' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': MYSQL_DB, 'USER': MYSQL_USER, 'PASSWORD': MYSQL_PASS, 'HOST': MYSQL_HOST_M, 'PORT': MYSQL_PORT, } } |
其中, APP_ACCESS, APP_SECRET, APP_NAME 都在sae的应用页可以看到,换成你应用的相关代码,然后执行 sync db 即可
执行完之后, True 改成 False