Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855358
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: 系统运维

2011-09-15 10:10:10

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)


阅读(609) | 评论(0) | 转发(0) |
0

上一篇:create models

下一篇:db query

给主人留下些什么吧!~~