django创建model
Posted yutingliuyl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django创建model相关的知识,希望对你有一定的参考价值。
django创建model
1. steps:
- new app
- add model class in app/models.py
- python manage.py migrate
2. model class
非常easy,在models.py里面定义一个继承models.Model的类就可以。
# Create your models here.
class Person(models.Model):
name = models.CharField(max_length=30)
然后python manage.py migrate
root@git:~/project/test03# python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, blog, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... 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 blog.0001_initial... OK
Applying sessions.0001_initial... OK
3. see the database
默认db配置在settings.py里面
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.sqlite3‘,
‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
}
}
能够看到用了sqlite。
sqlite3
root@git:~/project/test03# sqlite3 db.sqlite3
SQLite version 3.7.13 2012-06-11 02:05:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema blog_Person
CREATE TABLE "blog_person" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL);
sqlite>
manage.py syncdb了以后,自己主动创建了一些表和自己定义的model Person的数据表。
4. use mysql instead of sqlite
首先要安装mysql的python库
pip install mysql
创建mysql数据库
mysql> create database blog;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on blog.* to ‘aca‘@localhost identified by ‘123123‘;
Query OK, 0 rows affected (0.00 sec)
settings.py里面这么改
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘blog‘,
‘USER‘: ‘aca‘,
‘PASSWORD‘: ‘123123‘,
‘HOST‘: ‘localhost‘,
}
}
root@git:~/project/test03# python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, blog, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... 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 blog.0001_initial... OK
Applying sessions.0001_initial... OK
进到mysql 里面看,
mysql> desc blog_person
-> ;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
另一点就是django会生成一个auto increment的id字段。
之后就是用view 来显示出来了。
以上是关于django创建model的主要内容,如果未能解决你的问题,请参考以下文章