MultipleObjectMixin을 통한 ProjectApp 마무리 (작정하고 장고 41강)

_upper·2023년 7월 7일

41강

  • Project와 Article을 연결하는 작업, 그리고 MultipleObjectMixin 을 이용해서 ProjectApp 의 Detail 페이지를 마무리 하고, 같은 방식으로 AccountApp 의 디테일 페이지도 수정한다

댓글을 만들면선 for문을 돌 때 target_article에 foreignkey로 연결되어 있는 comment를 all로 불러와서 for문으로 돌리기

하지만 무조건 다 가져오는 것 문제고 복잡도를 높이는 것도 문제
여러가지 작업을 하고 싶을 땐 한계가 있음

해결을 위해 In View Using Mixin를 사용

여러가지 오브젝트를 다룰 수 있게 만들어주는 mixin이다

projectapp/views.py

class ProfileDetailView(DetailView, MultipleObjectMixin):
    model = Project
    context_object_name = 'target_project'
    template_name = 'projectapp/detail.html'

    paginate_by = 25

    def get_context_data(selfself, **kwargs):
        object_list = Article.objects.filter(project=self.get_object())
        return super(ProjectDetailView, self).get_context_data(object_list=object_list, **kwargs)

0개의 댓글