您好,登錄后才能下訂單哦!
這篇文章主要講解了Django ORM如何實現按天獲取數據去重求和,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
我就廢話不多說了,大家還是直接看代碼吧!
def total_data(request): data = request_body(request, 'POST') if not data: return http_return(400, '參數錯誤') # 前端傳入毫秒為單位的時間戳 startTimestamp = data.get('startTime', '') endTimestamp = data.get('endTime', '') if startTimestamp and endTimestamp: startTimestamp = int(startTimestamp/1000) endTimestamp = int(endTimestamp/1000) else: return http_return(400, '參數有誤') # 小于2019-05-30 00:00:00的時間不合法 if endTimestamp < startTimestamp or endTimestamp <= 1559145600 or startTimestamp <= 1559145600: return http_return(400, '無效時間') if startTimestamp and endTimestamp: # 給定時間查詢 startTime = datetime.fromtimestamp(startTimestamp) endTime = datetime.fromtimestamp(endTimestamp) t1 = datetime(startTime.year, startTime.month, startTime.day) t2 = datetime(endTime.year, endTime.month, endTime.day, 23, 59, 59, 999999) # 用戶總人數 totalUsers = User.objects.exclude(status='destroy').count() # 音頻總數 totalAudioStory = AudioStory.objects.filter(isDelete=False).count() # 專輯總數 totalAlbums = Album.objects.filter(isDelete=False).count() # 新增用戶人數 newUsers = User.objects.filter(createTime__range=(t1, t2)).exclude(status='destroy').count() # 活躍用戶人數 activityUsers = LoginLog.objects.filter(createTime__range=(t1, t2), isManager=False).values('userUuid_id').\ annotate(Count('userUuid_id')).count() # 新增音頻數 newAudioStory = AudioStory.objects.filter(createTime__range=(t1, t2)).count() # 男性 male = User.objects.filter(gender=1).exclude(status='destroy').count() # 女性 female = User.objects.filter(gender=2).exclude(status='destroy').count() # 未知 unkonwGender = User.objects.filter(gender=0).exclude(status='destroy').count() # 模板音頻 aduioStoryCount = AudioStory.objects.filter( isDelete=False, audioStoryType=1, isUpload=1, createTime__range=(t1, t2)).count() # 自由錄制 freedomStoryCount = AudioStory.objects.filter( isDelete=False, audioStoryType=0, isUpload=1, createTime__range=(t1, t2)).count() # 兒歌 tags1 = Tag.objects.filter(code="RECORDTYPE", name='兒歌').first() tags1Count = tags1.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).count() # 兒歌作品數 user1Count = tags1.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).\ values('userUuid_id').annotate(Count('userUuid_id')).count() # 錄音類型人數,去重 # result = Tag.objects.filter(code="RECORDTYPE").annotate(Count('tagsAudioStory')) # 父母學堂 tags2 = Tag.objects.filter(code="RECORDTYPE", name='父母學堂').first() tags2Count = tags2.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).count() user2Count = tags2.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).\ values('userUuid_id').annotate(Count('userUuid_id')).count() # 國學 tags3 = Tag.objects.filter(code="RECORDTYPE", name='國學').first() tags3Count = tags3.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).count() user3Count = tags3.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).\ values('userUuid_id').annotate(Count('userUuid_id')).count() # 英文 tags4 = Tag.objects.filter(code="RECORDTYPE", name='英文').first() tags4Count = tags4.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).count() user4Count = tags4.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)). \ values('userUuid_id').annotate(Count('userUuid_id')).count() # 其他 tags5 = Tag.objects.filter(code="RECORDTYPE", name='其他').first() tags5Count = tags5.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).count() user5Count = tags5.tagsAudioStory.filter(isDelete=False, createTime__range=(t1, t2)).\ values('userUuid_id').annotate(Count('userUuid_id')).count() recordTypePercentage = [ {'name': '兒歌', 'tagsNum': tags1Count, 'userNum': user1Count}, {'name': '兒歌', 'tagsNum': tags2Count, 'userNum': user2Count}, {'name': '國學', 'tagsNum': tags3Count, 'userNum': user3Count}, {'name': '英文', 'tagsNum': tags4Count, 'userNum': user4Count}, {'name': '其他', 'tagsNum': tags5Count, 'userNum': user5Count} ] # 活躍用戶排行 data1_list = [] # result = AudioStory.objects.filter(isDelete=False, createTime__range=(t1, t2)).values('userUuid_id').annotate(Count('userUuid_id'))[:1] res = User.objects.annotate(audioStory_count_by_user = Count("useAudioUuid")).order_by('-audioStory_count_by_user')[:5] for index,item in enumerate(res.values()): data = { 'orderNum': index+1, 'name': item['nickName'], 'recordCount': item['audioStory_count_by_user'] } data1_list.append(data) # 熱門錄制排行 data2_list = [] res = Story.objects.filter(status="normal", createTime__range=(t1, t2)).order_by('-recordNum')[:5] for index,item in enumerate(res.values()): data = { 'orderNum': index + 1 or -1, 'name': item['name'] or '', 'recordNum': item['recordNum'] or 0 } data2_list.append(data) # 熱門播放排行 data3_list = [] audioStory = AudioStory.objects.filter(isDelete=False, createTime__range=(t1, t2)).order_by('-playTimes')[:5] for index,item in enumerate(audioStory): data = { 'orderNum': index + 1, 'name': item.storyUuid.name if item.audioStoryType else item.name, 'playTimes': item.playTimes } data3_list.append(data) # 圖表數據--新增用戶 graph2 = User.objects.filter(createTime__range=(t1, t2)).\ extra(select={"time": "DATE_FORMAT(createTime,'%%Y-%%m-%%e')"}).\ order_by('time').values('time')\ .annotate(userNum=Count('createTime')).values('time', 'userNum') if graph2: graph2 = list(graph2) else: graph2 = [] # 活躍用戶 graph3 = LoginLog.objects.filter(createTime__range=(t1, t2), isManager=False). \ extra(select={"time": "DATE_FORMAT(createTime,'%%Y-%%m-%%e')"}). \ values('time').annotate(userNum=Count('createTime', distinct=True)).values('time', 'userNum') if graph3: graph3 = list(graph3) else: graph3 = [] return http_return(200, 'OK', { 'totalUsers': totalUsers, # 總用戶人數 'totalAudioStory': totalAudioStory, # 音頻總數 'totalAlbums': totalAlbums, # 總的專輯數 'newUsers': newUsers, # 新增用戶人數 'activityUsers': activityUsers, # 活躍用戶人數 'newAudioStory': newAudioStory, # 新增音頻數 'activityUsersRank': data1_list, # 活躍用戶排行 'male': male, # 男性 'female': female, # 女性 'unkonwGender': unkonwGender, # 未知性別 'aduioStoryCount': aduioStoryCount, # 模板音頻數量 'freedomStoryCount': freedomStoryCount, # 自由錄制音頻數量 'recordTypePercentage': recordTypePercentage, 'hotRecordRank': data2_list, # 熱門錄制排行 'hotPlayAudioStoryRank': data3_list, # 熱門播放排行 'newUserGraph': graph2, # 新增用戶折線圖 'activityUserGraph': graph3, # 活躍用戶折線圖 })
補充知識:Django 對符合條件的某個字段進行求和,聚合函數annotate()
開發環境:Ubuntu16.04+Django 1.11.9+Python2.7
對符合條件的某個字段求和
之前在開發的時候,有同事問Django是否存在著這樣的方法,可以直接將符合條件的某個字段直接求和.
當時不知道這樣的方法是否存在,但是想了想自己解決這類似問題的方法,先用filter將符合條件的取出來,然后進行for循環,取出需要的字段,進行求和.感覺是挺low的,于是一起Baidu,寫代碼測試最后找到了可以求值的方法,聚合函數annotate().
from django.db.models import Sum
from models import Book
all_price = Book.objects.values('price').annotate(num_books=Sum('price')).filter(author='Yu')
print all_price[0]['num_books']
輸出結果:650
上面的參數換個順序,不會出錯但不符合預期結果.
all_price = Book.objects.annotate(num_books=Sum('price')).filter(author='Yu').values('price')
print all_youxibi[0]['num_books']
輸出結果:'nums_book'
看完上述內容,是不是對Django ORM如何實現按天獲取數據去重求和有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。