BBS--功能4:個人站點頁面設計(ORM跨表與分組查詢)
阿新 • • 發佈:2018-11-22
.html closed have god trunc font .cn userinfo mon
查詢: 日期歸檔查詢 1 date_format ============date,time,datetime=========== create table t_mul_new(d date,t time,dt datetime); insert into t_mul_new values(now(),now(),now()); select * from t_mul; mysql> select * from t_mul; +------------+----------+---------------------+ | d | t | dt | +------------+----------+---------------------+ | 2017-08-01 | 19:42:22 | 2017-08-01 19:42:22 | +------------+----------+---------------------+ 1 row in總結set (0.00 sec) select date_format(dt,"%Y/%m/%d") from t_mul; 2 extra extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None) 有些情況下,Django的查詢語法難以簡單的表達復雜的 WHERE 子句,對於這種情況, Django 提供了 extra() QuerySet修改機制 — 它能在 QuerySet生成的SQL從句中註入新子句 extra可以指定一個或多個 參數,例如 select, whereor tables. 這些參數都不是必須的,但是你至少要使用一個!要註意這些額外的方式對不同的數據庫引擎可能存在移植性問題.(因為你在顯式的書寫SQL語句),除非萬不得已,盡量避免這樣做 參數之select The select 參數可以讓你在 SELECT 從句中添加其他字段信息,它應該是一個字典,存放著屬性名到 SQL 從句的映射。 queryResult=models.Article .objects.extra(select={‘is_recent‘: "create_time > ‘2017-09-05‘"}) 結果集中每個 Entry 對象都有一個額外的屬性is_recent, 它是一個布爾值,表示 Article對象的create_time 是否晚於2017-09-05. 練習: in sqlite: article_obj=models.Article.objects .extra(select={"standard_time":"strftime(‘%%Y-%%m-%%d‘,create_time)"}) .values("standard_time","nid","title") print(article_obj) # <QuerySet [{‘title‘: ‘MongoDb 入門教程‘, ‘standard_time‘: ‘2017-09-03‘, ‘nid‘: 1}]> 3 單表分組查詢 ...... 4 日期歸檔查詢的方式2 from django.db.models.functions import TruncMonth Sales.objects .annotate(month=TruncMonth(‘timestamp‘)) # Truncate to month and add to select list .values(‘month‘) # Group By month .annotate(c=Count(‘id‘)) # Select the count of the grouping .values(‘month‘, ‘c‘) # (might be redundant, haven‘t tested) select month and count
(1)用戶未找到,404頁面構建
# 個人站點頁面設計 re_path(r‘^(?P<username>\w+)$‘, views.home_site, name=‘home_site‘),
not_found.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/blog/bootstrap-3.3.7/css/bootstrap.css"> </head> <body> <div class="container" style="margin-top: 100px"> <div class="text-center"> <a href="http://www.cnblogs.com/"><img src="/static/img/logo_small.gif" alt="cnblogs"></a> <p><b>404.</b> 抱歉! 您訪問的資源不存在!</p> <p class="d">請確認您輸入的網址是否正確,如果問題持續存在,請發郵件至 [email protected] 與 <strong style="font-size: 28px">老村長</strong> 聯系。</p> <p><a href="/">返回網站首頁</a></p> </div> </div> </body> </html>View Code
(2)查詢當前站點對應的所有文章
def home_site(request,username): ‘‘‘ 個人站點視圖函數 :param request: :return: ‘‘‘ user=UserInfo.objects.filter(username=username).first() # 判斷用戶是否存在 if not user: return render(request,‘not_found.html‘) # 當前用戶或者當前站點所對應文章 blog=user.blog
print(blog)
# 方式一基於對象查詢 # 作者和文章的關系---> 一對多(文章) article_list=user.article_set.all() # 方式二 基於雙下劃線 __ 跨表查詢 article_list=models.Article.objects.filter(user=user) return render(request, ‘home_site.html‘)
2、個人站點標簽與分類查詢
# 跨表的分組查詢的模型: # 每一個後的表模型.objects.values("pk").annotate(聚合函數(關聯表__統計字段)).values("表模型的所有字段以及統計字段") # 推薦pk字段查找 # 查詢每一個分類名稱以及對應的文章數 # 查詢當前站點的每一個分類名稱以及對應的文章數 # 查詢當前站點的每一個標簽名稱以及對應的文章數 # 查詢當前站點每一個年月的名稱以及對應的文章數
from django.db.models import Avg, Max, Min, Sum, Count
# 補充知識點Emp.objects.all()-----select * from emp # 補充知識點Emp.objects.all().values(‘name‘)-----select name from emp
# values(‘province‘)---values(‘group by的字段‘)
values方法可以獲取字段的字典列表。
values_list可以獲取字段的元組列表。
(1)查詢每一個分類名稱以及對應的文章數
# 查詢每一個分類名稱以及對應的文章數 # annotate(聚合函數(關聯表__統計字段)).values("表模型的所有字段以及統計字段") # values(‘group by的字段‘) ret=models.Category.objects.values(‘pk‘).annotate(c=Count(‘article__title‘)).values(‘title‘,‘c‘) print(ret)
(2) 查詢當前站點的每一個標簽名稱以及對應的文章數
# 查詢當前站點的每一個分類名稱以及對應的文章數 cate_list=models.Category.objects.filter(blog=blog).values(‘pk‘).annotate(c=Count(‘article__title‘)).values(‘title‘,‘c‘) print(cate_list)
(3)每一個標簽以及對應得文章數
# 每一個標簽以及對應得文章數 tag_list=models.Tag.objects.filter(blog=blog).values(‘pk‘).annotate(count=Count(‘article‘)).values_list(‘title‘,‘count‘) print(‘tag_list‘,tag_list)
(4)日期歸檔查詢
知識點1date_format
create table t_mul_new(d date,t time,dt datetime);
insert into t_mul_new values(now(),now(),now()); select * from t_mul_new;
---
知識點2:extra
BBS--功能4:個人站點頁面設計(ORM跨表與分組查詢)