日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Django搭建简易博客教程(四)-Models

發(fā)布時(shí)間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Django搭建简易博客教程(四)-Models 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文鏈接:

http://www.jianshu.com/p/dbc4193b4f95

Django Model


  • 每一個(gè)Django Model都繼承自django.db.models.Model
  • 在Model當(dāng)中每一個(gè)屬性attribute都代表一個(gè)database field
  • 通過Django Model API可以執(zhí)行數(shù)據(jù)庫的增刪改查, 而不需要寫一些數(shù)據(jù)庫的查詢語句

設(shè)置數(shù)據(jù)庫

Django項(xiàng)目建成后, 默認(rèn)設(shè)置了使用SQLite數(shù)據(jù)庫, 在my_blog/my_blog/setting.py中可以查看和修改數(shù)據(jù)庫設(shè)置:

DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR, 'db.sqlite3'),} }

還可以設(shè)置其他數(shù)據(jù)庫, 如MySQL, PostgreSQL, 現(xiàn)在為了簡(jiǎn)單, 使用默認(rèn)數(shù)據(jù)庫設(shè)置

創(chuàng)建models


在my_blog/article/models.py下編寫如下程序:

from django.db import models# Create your models here. class Article(models.Model) :title = models.CharField(max_length = 100) #博客題目category = models.CharField(max_length = 50, blank = True) #博客標(biāo)簽date_time = models.DateTimeField(auto_now_add = True) #博客日期content = models.TextField(blank = True, null = True) #博客文章正文def __unicode__(self) :return self.titleclass Meta: #按時(shí)間下降排序ordering = ['-date_time']

其中__unicode__(self)?函數(shù)Article對(duì)象要怎么表示自己, 一般系統(tǒng)默認(rèn)使用<Article: Article object>?來表示對(duì)象, 通過這個(gè)函數(shù)可以告訴系統(tǒng)使用title字段來表示這個(gè)對(duì)象

  • CharField?用于存儲(chǔ)字符串, max_length設(shè)置最大長度
  • TextField?用于存儲(chǔ)大量文本
  • DateTimeField?用于存儲(chǔ)時(shí)間, auto_now_add設(shè)置True表示自動(dòng)設(shè)置對(duì)象增加時(shí)間

同步數(shù)據(jù)庫


$ python manage.py migrate #命令行運(yùn)行該命令

因?yàn)槲覀円呀?jīng)執(zhí)行過該命令會(huì)出現(xiàn)如下提示

Operations to perform:Apply all migrations: admin, contenttypes, sessions, auth Running migrations:No migrations to apply.Your models have changes that are not yet reflected in a migration, and so won't be applied.Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

那么現(xiàn)在需要執(zhí)行下面的命令

$ python manage.py makemigrations #得到如下提示 Migrations for 'article':0001_initial.py:- Create model Article

現(xiàn)在重新運(yùn)行以下命令

$ python manage.py migrate #出現(xiàn)如下提示表示操作成功 Operations to perform:Apply all migrations: auth, sessions, admin, article, contenttypes Running migrations:Applying article.0001_initial... OK

migrate命令按照app順序建立或者更新數(shù)據(jù)庫, 將models.py與數(shù)據(jù)庫同步

Django Shell


現(xiàn)在我們進(jìn)入Django中的交互式shell來進(jìn)行數(shù)據(jù)庫的增刪改查等操作

$ python manage.py shell Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 5 2014, 20:42:22) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>>

這里進(jìn)入Django的shell和python內(nèi)置的shell是非常類似的

>>> from article.models import Article >>> #create數(shù)據(jù)庫增加操作 >>> Article.objects.create(title = 'Hello World', category = 'Python', content = '我們來做一個(gè)簡(jiǎn)單的數(shù)據(jù)庫增加操作') <Article: Article object> >>> Article.objects.create(title = 'Django Blog學(xué)習(xí)', category = 'Python', content = 'Django簡(jiǎn)單博客教程') <Article: Article object>>>> #all和get的數(shù)據(jù)庫查看操作 >>> Article.objects.all() #查看全部對(duì)象, 返回一個(gè)列表, 無對(duì)象返回空list [<Article: Article object>, <Article: Article object>] >>> Article.objects.get(id = 1) #返回符合條件的對(duì)象 <Article: Article object>>>> #update數(shù)據(jù)庫修改操作 >>> first = Article.objects.get(id = 1) #獲取id = 1的對(duì)象 >>> first.title 'Hello World' >>> first.date_time datetime.datetime(2014, 12, 26, 13, 56, 48, 727425, tzinfo=<UTC>) >>> first.content '我們來做一個(gè)簡(jiǎn)單的數(shù)據(jù)庫增加操作' >>> first.category 'Python' >>> first.content = 'Hello World, How are you' >>> first.content #再次查看是否修改成功, 修改操作就是點(diǎn)語法 'Hello World, How are you'>>> #delete數(shù)據(jù)庫刪除操作 >>> first.delete() >>> Article.objects.all() #此時(shí)可以看到只有一個(gè)對(duì)象了, 另一個(gè)對(duì)象已經(jīng)被成功刪除 [<Article: Article object>]

當(dāng)然還有更多的API, 可以查看官方文檔

轉(zhuǎn)載于:https://www.cnblogs.com/saolv/p/7044806.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Django搭建简易博客教程(四)-Models的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。