簡單的部落格系統(二)Django編寫資料模型類
阿新 • • 發佈:2019-07-21
設計資料庫和表結構是做網站的基礎。在Django中,不需要通過SQL語句直接跟資料庫打交道,而是完全用Python的類來建立資料模型,之後交給Django完成建立資料庫的操作。
資料模型類
資料模型類需要在 應用目錄 下的
models.py
檔案中編寫
編寫資料模型
- 下面的程式碼演示了在 models.py 中定義了一個部落格文章的類
from django.db import models from django.utils import timezone from django.contrib.auth.models import User # Create your models here. class BlogArticles(models.Model): # Django中的資料模型類都繼承自 django.db.models.Model類 # 欄位 title 的屬性為 CharField 型別,並且引數長度為 300 title = models.CharField(max_length=300) """ 欄位 author 使用 ForeignKey 規定了部落格文章和使用者之間的關係:一個使用者對應多篇文章 models.CASCADE 表示級聯刪除 related_name="blog_posts" 表示允許User類的例項以 "blog_posts" 屬性反向查詢到BlogArticles類的例項 """ author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="blog_posts") body = models.TextField() publish = models.DateTimeField(default=timezone.now) """ ordering = ("-publish",) 規定BlogArticles類的例項按 publish 欄位值倒序顯示 """ class Meta: ordering = ("-publish",) """ 重寫父類的方法,使得當使用 str()函式轉換該類的例項為字串時,返回 title 屬性的值 """ def __str__(self): return self.title
根據資料模型建立資料庫表
- 建立資料庫表文件
E:\PycharmProjects\demosite>python manage.py makemigrations
Migrations for 'blog':
blog\migrations\0002_auto_20190720_1919.py
- Alter field publish on blogarticles
- 上面執行結果的提示資訊中,告訴我們在
blog/migrations
目錄中建立了一個 BlogArticles模型,模型編號為 0001。可以輸入以下命令檢視相對應的SQL語句:
E:\PycharmProjects\demosite>python manage.py sqlmigrate blog 0001 System check identified some issues: WARNINGS: blog.BlogArticles.publish: (fields.W161) Fixed default value provided. HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now` BEGIN; -- -- Create model BlogArticles -- CREATE TABLE "blog_blogarticles" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(300) NOT NULL, "body" text NOT NULL, "publish" datetime NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE INDEX "blog_blogarticles_author_id_ed798e23" ON "blog_blogarticles" ("author_id"); COMMIT;
在資料庫中表名格式為:
小寫的應用名稱_小寫的類名稱
- 建立資料庫
(demosite) E:\PycharmProjects\demosite>python manage.py migrate
System check identified some issues:
WARNINGS:
blog.BlogArticles.publish: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... 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 auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying blog.0001_initial... OK
Applying sessions.0001_in