시간 날 때마다 django 공식 문서 꼼꼼히 읽어보는 게 중요하다.
모르는 기능이 있으면 찾아보기.
from django.urls import path, include
from users import views
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
urlpatterns = [
path('signup/', views.UserView.as_view(), name='user_view'),
path('mock/', views.mockView.as_view(), name='mock_view'),
path('api/token/', views.CustomTokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('<int:user_id>/', views.ProfileView.as_view(), name="profile_view"), # 추가
]
이제 feed_view를 만든다.
class ProfileView(APIView):
# permission_classes = [permissions.IsAuthenticated] 프로필 페이지 로그인 안 해도 볼 수 있다
def get(self, request, user_id):
user = get_object_or_404(User, id=user_id) # 유저 가져오기: 유저 정보 보기 위해
serializer = UserProfileSerializer(user) # 시리얼라이저 가져오기: () 안에 유저 정보
return Response(serializer.data)
작성한 글 확인이 가능하다.
# users/serializers.py
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework import serializers
from users.models import User
from articles.serializers import ArticleListSerializer
class UserProfileSerializer(serializers.ModelSerializer):
# followers = serializers.PrimaryKeyRelatedField(many=True, read_only=True) # id
followers = serializers.StringRelatedField(many=True)
followings = serializers.StringRelatedField(many=True) # 이메일로 표현되게
article_set = ArticleListSerializer(many=True)
like_articles = ArticleListSerializer(many=True)
class Meta:
model = User
fields = ("id", "email", "followings", "followers", "article_set", "like_articles")
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
# 본인일 때: 옵션 설정 symmetrical대칭=False (인스타) 일방적. 맞팔이 의무가 아님
followings = models.ManyToManyField('self', symmetrical=False, related_name='followers', blank=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class UserAdmin(BaseUserAdmin):
form = UserChangeForm # 위에 있는 클래스
add_form = UserCreationForm
list_display = ('id', 'email', 'is_admin')
list_filter = ('is_admin',) # 무엇으로 필터할 수 있는지
fieldsets = (
(None, {'fields': ('email', 'password', 'followings')}),
('Permissions', {'fields': ('is_admin',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password1', 'password2'),
}),
)
search_fields = ('email',)
ordering = ('email',)
filter_horizontal = ()
admin.site.register(User, UserAdmin)
admin.site.unregister(Group)
# GET, {{host}}/users/8
{
"id": 8,
"email": "rafaela@gmail.com",
"followings": [
2,
3,
4,
8
],
"followers": [
"rafaela@gmail.com",
"grace@gmail.com"
],
"article_set": [
{
"pk": 3,
"title": "postman으로 수정 중",
"image": null,
"updated_at": "2022-11-12T10:37:52.690077Z",
"user": "rafaela@gmail.com",
"likes_count": 2,
"comments_count": 1
},
{
"pk": 5,
"title": "postino로 작성 중",
"image": null,
"updated_at": "2022-11-12T10:37:42.156111Z",
"user": "rafaela@gmail.com",
"likes_count": 3,
"comments_count": 1
}
],
"like_articles": [
{
"pk": 5,
"title": "postino로 작성 중",
"image": null,
"updated_at": "2022-11-12T10:37:42.156111Z",
"user": "rafaela@gmail.com",
"likes_count": 3,
"comments_count": 1
}
]
}