Django doc summary (2)
2015-12-10 22:31
387 查看
Database
Default database
In the project setting file 'samsite/settings.py', there are some lines' code about the db.DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
The default db is sqlite3. You can select another db which you like, but you should modify some settings, such as 'engine', user, password, etc.
Creat models
samapp/models.pyfrom django.db import models # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
So, we have two models and each model has some fileds.
Activating models
Edit the samsite/settings.py to let Django know to include the samapp app.INSTALLED_APPS = [ 'samapp.apps.SamappConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
By running makemigrations, you're telling Django that you have some changes to your models and that you'd like the changes to be stored as a migration.
D:\SamProject\django\samsite>python manage.py makemigrations samapp Migrations for 'samapp': 0001_initial.py: - Create model Choice - Create model Question - Add field question to choice
As we use sqlite, you can use below cmd to view the sql.
D:\SamProject\django\samsite>python manage.py sqlmigrate samapp 0001 BEGIN; -- -- Create model Choice -- CREATE TABLE "samapp_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL); -- -- Create model Question -- CREATE TABLE "samapp_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL); -- -- Add field question to choice -- ALTER TABLE "samapp_choice" RENAME TO "samapp_choice__old"; CREATE TABLE "samapp_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "samapp_question" ("id")); INSERT INTO "samapp_choice" ("choice_text", "id", "votes", "question_id") SELECT "choice_text", "id", "votes", NULL FROM "samapp_choice__old"; DROP TABLE "samapp_choice__old"; CREATE INDEX "samapp_choice_7aa0f6ee" ON "samapp_choice" ("question_id"); COMMIT;
Finally, we use the bellow cmd to create those model tables in your db.
D:\SamProject\django\samsite>python manage.py migrate Operations to perform: Apply all migrations: sessions, admin, auth, contenttypes, samapp 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 samapp.0001_initial... OK Applying sessions.0001_initial... OK
相关文章推荐
- Django doc summary (1)
- MFC-SetWindowPos-位图按钮-扩展对话框-更改Logo
- 将语音搜索集成到Google Now中
- django上传文件原理解析
- OC block Category
- Django学习笔记<三>
- BestCoder Round #53 (div.2) C.Rikka with Graph II(dfs)
- 什么是测试开发工程师-google的解释
- windows下构建Go程序
- google map
- go的Type switch是一个switch语句么?
- 简单 Goroutine 池实现
- Django教程笔记:六、中间件middleware
- 毕业设计——001Google搜索
- [django-xadmin]如何在 search_fields和list_filter 中包含外键字段
- GoEasy在web端推送及接收
- Codeforces 437E The Child and Polygon(间隔DP)
- LeeCode Algorithm #3 Longest Substring Without Repeating Characters
- Google的Guava它Collection升华
- Getting Started with Django Logging in 5 Minutes