免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1795 | 回复: 0
打印 上一主题 下一主题

set a project with mysql --simple sample [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 02:51 |只看该作者 |倒序浏览
1. django-admin startproject django_test
2. vim settings.py
change the database session.
DATABASES = {
    'default': {
        'ENGINE': 'mysql',#'django.db.backends.',  Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'django_test',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite
3. django-admin startapp django_web
4. vim models.py
from django.db import models

# Create your models here.

class TestGroup(models.Model):
    test_group = models.CharField(max_length=100, blank = True)
    component = models.CharField(max_length=100, blank = True, unique = True)

    class Meta:
        db_table = 'test_group'
    def __unicode__(self):
        return self.test_group
5. add 'django_test.django_web' to settings.py
6. ./manage.py validate
this will varify the syntax of settings.py and models.py
7.  ./manage.py sql django_web
show the sql statement from models
something like
  1. BEGIN;CREATE TABLE `test_group` (
  2.     `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
  3.     `test_group` varchar(100) NOT NULL,
  4.     `component` varchar(100) NOT NULL UNIQUE
  5. )
  6. ;COMMIT;

8.  ./manage.py syncdb
create 'test_group' table and other tables under django_test database.

  1. describe test_group;
  2. +------------+--------------+------+-----+---------+----------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +------------+--------------+------+-----+---------+----------------+
  5. | id | int(11) | NO | PRI | NULL | auto_increment |
  6. | test_group | varchar(100) | NO | | NULL | |
  7. | component | varchar(100) | NO | UNI | NULL | |
  8. +------------+--------------+------+-----+---------+----------------+
  9. 3 rows in set (0.00 sec)


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP