Django数据库模型生成数据表

django是通过ORM访问数据库的,所以,它需要自己定义数据模型。

打开相应app/models.py

1
2
3
4
5
6
7
8
9
class UserMessage(models.Model):
name = models.CharField(verbose_name=u"名字", max_length=50)
email = models.EmailField(verbose_name=u"邮箱地址")
address = models.CharField(verbose_name=u"联系地址", max_length=100)
message = models.CharField(verbose_name=u"留言", max_length=100)

class Meta:
verbose_name = u'用户记录'
verbose_name_plural = verbose_name

定义字段,字段类型,长度,描述名。
除了上诉的字符类型,还有以下的数据类型可以定义

1
2
3
4
5
6
7
8
9
10
__all__ = [str(x) for x in (
'AutoField', 'BLANK_CHOICE_DASH', 'BigIntegerField', 'BinaryField',
'BooleanField', 'CharField', 'CommaSeparatedIntegerField', 'DateField',
'DateTimeField', 'DecimalField', 'DurationField', 'EmailField', 'Empty',
'Field', 'FieldDoesNotExist', 'FilePathField', 'FloatField',
'GenericIPAddressField', 'IPAddressField', 'IntegerField', 'NOT_PROVIDED',
'NullBooleanField', 'PositiveIntegerField', 'PositiveSmallIntegerField',
'SlugField', 'SmallIntegerField', 'TextField', 'TimeField', 'URLField',
'UUIDField',
)]

<path-to-your-project>/settings.py
添加自己的app到INSTALLED_APPS

1
2
3
4
5
6
7
8
9
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'message',
]

然后运行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
manage.py@opencourse > makemigrations message
Migrations for 'message':
0001_initial.py:
- Create model UserMessage
Following files were affected
/g/project/opencourse/apps/message/migrations/0001_initial.py
Process finished with exit code 0
manage.py@opencourse > migrate message
migrate message /g/project/opencourse"
Operations to perform:
Apply all migrations: message
Running migrations:
Rendering model states... DONE
Applying message.0001_initial... OK

Process finished with exit code 0

这个时候就可以看到数据库已经生成了一个新的表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
MySQL [opencourse]> show tables;
+----------------------------+
| Tables_in_opencourse |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| message_usermessage |
+----------------------------+
11 rows in set (0.00 sec)

MySQL [opencourse]> desc message_usermessage;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| email | varchar(254) | NO | | NULL | |
| address | varchar(100) | NO | | NULL | |
| message | varchar(100) | NO | | NULL | |
+---------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

MySQL [opencourse]>

除此之外,还可以定义索引,自定义表名等。

##参考资料

Model field reference
Model Meta options

关注公众号 尹安灿