2016-11-08 1 views
0

모델이다. 이 같은 자습서에 의해 자원 클래스 제작 :장고 tastypie 상대의 검색어

class ProjectResource(ModelResource): 
    class Meta: 
     queryset = Project.objects.all() 
     resource_name = 'project' 

그러나 모든 사용자에 대해 모든 데이터를 반환이 코드를.

인증을 추가하는 방법을 알고 있지만 모든 프로젝트를 반환하는 방법을 이해할 수는 없지만 로그인 된 사용자 ID를 기반으로 일부 데이터는이 하위 집합에 포함됩니다.

답변

1

당신은 get_object_list

class ProjectResource(ModelResource): 
    class Meta: 
     queryset = Project.objects.all() 
     resource_name = 'project' 

    def get_object_list(self, request): 
     qs = super().get_object_list(request) 
     return qs.filter(creator=request.user) 

    def authorized_read_list(self, object_list, bundle): 
     return object_list.filter(creator=bundle.request.user.id) 
+0

아를 재정의 할 수 있습니다, 감사합니다! 앞으로 folls 시간을 절약하기 위해 authorized_read_list 함수도 있습니다 ... – KaronatoR

+0

@KaronatoR 더 적합한 대답이 있다면 광산을 편집하고 개선 사항을 수락합니다. –