您好,登錄后才能下訂單哦!
本篇內容介紹了“Django怎么編寫數據模型類”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
設計數據庫和表結構是做網站的基礎。在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_initial... OK
“Django怎么編寫數據模型類”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。