django创建model
1. steps:
- new app
- add model class in app/models.py
- python manage.py migrate
2. model class
很简单,在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 来显示出来了。
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-16 05:36:20