django: db howto - 1

时间:2024-06-11 08:07:19

以在 Django 中使用 MySQL 为例,首先要安装 MySQL 和 MySQL-python 组件,确保 python 能执行 import MySQLdb。

MySQL 中创建数据库:

[root@bogon csvt03]#  mysql -uroot -p
Enter password: mysql> create database csvt default charset utf8;
Query OK, row affected (0.01 sec) mysql>

创建工程 csvt03,并修改 csvt03/settings.py 中的数据库设置:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'csvt', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'wdlinux.cn',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}

创建应用 blog 并修改 csvt03/settings.py 中的应用设置,加入 blog 应用:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)

Django 中使用 ORM(对象关系模型)来操作数据库,数据库操作的关键类是:django.db.models.Model。

在 blog/models.py 中实验数据库操作如下:

from django.db import models

class Employee(models.Model):
name
= models.CharField(max_length=20) # map 'name' field to db

同步数据库:

[root@bogon csvt03]#  python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table blog_employee You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no
Installing custom SQL ...
Installing indexes ...
Installed object(s) from fixture(s)
[root@bogon csvt03]#

在 MySQL 中查看同步结果:

mysql> use csvt;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show tables;
+----------------------------+
| Tables_in_csvt |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| blog_employee |
| django_content_type |
| django_session |
| django_site |
+----------------------------+
rows in set (0.00 sec)

mysql> desc blog_employee;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql>

可见 Employee 类中的 name 所对应的数据库字段已经自动生成了。