- count
- migrations
- M:N
- delete
- 그리디
- regexp
- 백트래킹
- 이진트리
- 완전검색
- create
- outer join
- update
- 뷰
- N:1
- drf
- 통계학
- Vue
- ORM
- 큐
- 쟝고
- distinct
- DB
- Tree
- 스택
- stack
- 트리
- Django
- SQL
- Queue
- Article & User
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
목록DB (26)
데이터 분석 기술 블로그
문제 상황prefetch_related 적용문제 해결 1단계입니다.게시글을 조회하면서 참조된 댓글까지 한 번에 조회합니다.# views.pydef index_4(request): # articles = Article.objects.order_by('-pk') articles = Article.objects.prefetch_related('comment_set').order_by('-pk') # articles = Article.objects.prefetch_related( # Prefetch('comment_set', queryset=Comment.objects.select_related('user')) # ).order_by('-pk') "111 queries includin..
prefetch_relatedM:N 또는 N:1 역참조 관계에서 사용합니다. SQL이 아닌 Python을 사용한 JOIN을 진행합니다.문제 상황prefetch_related 적용문제를 해결해 봅니다.게시글을 조회하면서 참조된 댓글까지 한 번에 조회해서 가져옵니다.# views.pydef index_3(request): # articles = Article.objects.order_by('-pk') articles = Article.objects.prefetch_related('comment_set').order_by('-pk') context = { 'articles': articles, } return render(request, 'articles/index_3...
select_relatedSQL의 INNER JOIN 쿼리를 활용합니다. 1:1 또는 N:1 참조 관계에서 사용합니다.문제 상황select_related 적용문제를 해결해 봅니다.게시글을 조회하면서 유저 정보까지 한 번에 조회해서 가져옵니다.# views.pydef index_2(request): # articles = Article.objects.order_by('-pk') articles = Article.objects.select_related('user').order_by('-pk') context = { 'articles': articles, } return render(request, 'articles/index_2.html', context)"11 qu..
annotateSQL의 GROUP BY 쿼리를 사용합니다.문제 상황annotate 적용문제를 해결해 봅시다.게시글을 조회하면서 댓글 개수까지 한 번에 조회해서 가져옵니다.# views.pydef index_1(request): # articles = Article.objects.order_by('-pk') articles = Article.objects.annotate(Count('comment')).order_by('-pk') context = { 'articles': articles, } return render(request, 'articles/index_1.html', context)댓글 개수 : {{ article.comment__count }}"11 qu..
Improve query같은 결과를 얻기 위해 DB 측에 보내는 쿼리 개수를 점차 줄여 조회합니다.1. 사전 준비데이터게시글 10개 / 댓글 100개 / 유저 5개모델 관계N:1 - Article:User / Comment:Article / Comment:ArticleN:M - Article:Userpython manage.py migratepython manage.py laoddata users.json articles.json comments.jsonInstalled 115 object(s) from 3 fixture(s)2. 종류annotateselect_relatedprefetch_relatedselect_related & prefetch_related
1. FixturesDjango가 데이터베이스로 가져오는 방법을 알고 있는 데이터 모음입니다. 데이터베이스 구조에 맞추어 작성되어있습니다.2. 초기 데이터 제공Fixtures의 사용 목적입니다.3. Fixtures 활용3-1 사전 준비M:N까지 모두 작성된 Django 프로젝트에서 유저, 게시글, 댓글 등 각 데이터를 최소 2~3개 이상 생성해 둡니다.3-2 fixtures 관련 명령어dumpdata: 생성 (데이터 추출)loaddata: 로드 (데이터 입력)3-3 dumpdata데이터베이스의 모든 데이터를 추출합니다. 추출한 데이터는 json 형식으로 저장합니다.# 작성 예시python manage.py dumpdata [app_name[.ModelName] [app_name[.ModelName] ....
User(M) - User(N)0명 이상의 회원은 0명 이상의 회원과 관련이 있습니다. 즉, 회원은 0명 이상의 팔로워를 가질 수 있고, 0명 이상의 다른 회원들을 팔로잉할 수 있습니다.ManyToManyFiled를 작성합니다.참조 : 내가 팔로우하는 사람들(팔로잉, followings)역참조 : 상대방 이밪에서 나는 팔로워 중 한 명 (팔로워, followers)바뀌어도 상관 없으나 관계 조회 시 생각하기 편한 방향으로 정한 것입니다.# accounts/models.pyclass User(AbstractUser): followings = models.ManyToManyField('self', symmetraicl=False, related_name='followers')Migrations 진행 후..
프로필 페이지각 회원의 개인 프로필 페이지에 팔로우 기능을 구현하기 위해 프로필 페이지를 먼저 구현해 봅시다.url을 작성합니다.# accounts/urls.pyurlpatterns = [ ... path('profile//', views.profile, name='profile'),]view 함수를 작성합니다.# accounts/views.pyfrom django.contrib.auth import get_user_modeldef profile(request, username): User = get_user_model() person = User.objects.get(username=username) context = { 'person': person, } ..