数据库创建

创建数据库ALU(和alu02/settings.py中一致)

mysql> create database ALU;
Query OK, 1 row affected (0.01 sec)

mysql> 

 

在django中是通过定义models.py来创建数据表的

[root@host-100-100-5-17 alu02]# cat blog/models.py
from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length = 20)
[root@host-100-100-5-17 alu02]# 

 

数据库关联

[root@host-100-100-5-17 alu02]# python manage.py makemigrations
/root/ALU/alu02/alu02/urls.py:21: RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got blog.views.index). Pass the callable instead.
  url(r'^blog/index$', 'blog.views.index'),

Migrations for 'blog':
  0001_initial.py:
    - Create model Employee
[root@host-100-100-5-17 alu02]# python manage.py migrate
/root/ALU/alu02/alu02/urls.py:21: RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got blog.views.index). Pass the callable instead.
  url(r'^blog/index$', 'blog.views.index'),

Operations to perform:
  Apply all migrations: admin, blog, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying blog.0001_initial... OK
  Applying sessions.0001_initial... OK
[root@host-100-100-5-17 alu02]# 

 

 检查数据库中ALU.blog_employee被创建

mysql> use ALU
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_ALU              |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| blog_employee              |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
11 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>

 

你可能感兴趣的:(数据库创建)