數據總覽功能,是對網站中產生的數據進行一個統計,統計出視頻數、發佈數、用戶數、評論數,等等。讓管理者對網站數據有一個清晰的認識,作到心中有數。html
在本站中,筆者一共列舉了下面幾種數據:視頻數、發佈中 未發佈、用戶數、用戶新增、評論數、評論新增,等幾項內容。django
咱們把全部的數據都封裝到了一個函數裏面,即 IndexView 它位於後臺管理的首頁。bash
path('', views.IndexView.as_view(), name='index'),
複製代碼
IndexView代碼以下ide
class IndexView(AdminUserRequiredMixin, generic.View):
def get(self, request):
video_count = Video.objects.get_count()
video_has_published_count = Video.objects.get_published_count()
video_not_published_count = Video.objects.get_not_published_count()
user_count = User.objects.count()
user_today_count = User.objects.exclude(date_joined__lt=datetime.date.today()).count()
comment_count = Comment.objects.get_count()
comment_today_count = Comment.objects.get_today_count()
data = {"video_count": video_count,
"video_has_published_count": video_has_published_count,
"video_not_published_count": video_not_published_count,
"user_count": user_count,
"user_today_count": user_today_count,
"comment_count": comment_count,
"comment_today_count": comment_today_count}
return render(self.request, 'myadmin/index.html', data)
複製代碼
與視頻相關的統計,咱們封裝到了Video的models.py下面,函數
class VideoQuerySet(models.query.QuerySet):
# 視頻總數
def get_count(self):
return self.count()
# 發佈數
def get_published_count(self):
return self.filter(status=0).count()
# 未發佈數
def get_not_published_count(self):
return self.filter(status=1).count()
複製代碼
以上數據,大都使用了filter過濾器進行了過濾,最後經過count()函數返回給業務方。網站
與用戶相關的統計,咱們直接經過count和exclude將相關數據過濾出來。ui
與評論相關的統計,封裝到了Comment的models.py下面,spa
class CommentQuerySet(models.query.QuerySet):
# 評論總數
def get_count(self):
return self.count()
# 今日新增
def get_today_count(self):
return self.exclude(timestamp__lt=datetime.date.today()).count()
複製代碼
其中,今日新增評論,咱們經過exclude來過濾時間,使用了 lt 標籤來過濾。更多標籤的使用方法,可參考 官方文檔code