DB M:N(User-User)

Luca·2023년 4월 12일
0

DB

목록 보기
3/3
post-thumbnail

M:N(User-User)
User 자기 자신과의 M:N 관계 설정을 통한 팔로우 기능 구현

Profile 구현

  1. url 작성
  2. views 작성
    from django.contrib.auth import get_user_model
  3. templates/profile.html 작성
  4. Profile 템플릿으로 이동할 수 있게 base.html과 index.html에 링크 생성
    user.username 과 article.user.username의 차이
  5. 결과물

Follow 구현

  1. 모델 관계 설정
    당연하게 makemigrations, migrate 진행 생성된 중개 테이블 확인
    • followings -> 정참조 followers -> 역참조
  2. url 작성
  3. views 작성 + decorations 진행
    filter()메서드는 검색을 필터링하는 데 사용되며 검색어와 일치하는 행만 반환
    로그인이 안되어 있으면 로그인 페이지로 이동하게
  4. profile에서 팔로잉, 팔로워 수 & 팔로우, 언팔로우 버튼 작성
    팔로잉 : {{ person.following.all | length }}
    팔로워 : {{ person.followers.all | length }}
  5. 결과물 (팔로우버튼을 누르면 언팔로우라는 버튼도 나오게)

0개의 댓글