Notice
Recent Posts
Recent Comments
Link
Ssul's Blog
django 이번주 월요일 찾기 > 일주일 쿼리셋 구해서 > 직렬화해서 클라이언트 응답 본문
오늘 날짜를 기준으로 이번주 월요일 찾고,
이번주 쿼리셋 출력하기
#1. 오늘 날짜 기준으로 이번주 월요일 찾기
import datetime
today = datetime.date.today()
print(today) # 2022-10-05 오늘날짜
print(today.weekday()) #월요일의 일자
monday = today - datetime.timedelta(days=today.weekday())
print(monday) #2022-10-03
sunday = monday + datetime.timedelta(days=6)
print(sunday) #2022-10-09
#2. 이번주 로그 범위로 검색해서 쿼리셋 직렬화
#로그 생성일 기준 1주일 로그 가져오기(__range)
walletlogs = self.wallet_logs.filter(created_at__range=[monday,sunday])
#직렬화(예)
json_walletlogs = serializers.serialize('json', walletlogs)
(직접 커스터마이징 버전)
walletlogs = self.wallet_logs.filter(created_at__range=[monday,sunday])
data = [
{'date': walletlog.created_at, 'habitordairy': walletlog.type, 'income': walletlog.income } for walletlog in walletlogs
]
json_walletlogs = data
return (
json_walletlogs
)
'dev > 기능구현' 카테고리의 다른 글
React Native 구글 소셜로그인 구현 (1) | 2023.02.12 |
---|---|
아임포트로 구독결제 구현하기(django, react, restframework) (0) | 2023.02.03 |
게시판 검색기능 구현(검색어 입력 & 버튼값으로) (0) | 2020.09.27 |
[자기관리 어플Proj] 2일차 회원가입 및 자기관리 백앤드 개발 (0) | 2020.09.15 |
[자기관리 어플Proj] 1일차 모델설계 (0) | 2020.09.15 |