2012-11-23 2 views
3

Django with Tastypie에는 개체 세부 정보 만 표시하도록 리소스를 구성하는 방법이 있습니까?Django Tastypie - 개체 정보 만있는 리소스

단일 사용자 개체가 포함 된 목록과 달리 인증 된 사용자의 세부 정보를 반환하는 URL을 /user 갖고 싶습니다. 나는 사용자의 세부 사항을 얻기 위해 /users/<id>을 사용하고 싶지 않다.

여기 내 코드의 관련 부분입니다 :

from django.contrib.auth.models import User 
from tastypie.resources import ModelResource 

class UserResource(ModelResource): 

    class Meta: 
     queryset  = User.objects.all() 
     resource_name = 'user' 
     allowed_methods = ['get', 'put'] 
     serializer  = SERIALIZER  # Assume those are defined... 
     authentication = AUTHENTICATION # " 
     authorization = AUTHORIZATION # " 

    def apply_authorization_limits(self, request, object_list): 
     return object_list.filter(pk=request.user.pk) 
+0

에 대한 자세한 내용이 설명되어 있습니다? 귀하의 코드를 뿌리십시오 - [obj_get] (http : // django-tastypie) 대신 [obj_get_list] (http://django-tastypie.readthedocs.org/en/latest/resources.html#id9) .readthedocs.org/ko/latest/resources.html # id10). – Tadeck

+0

@Tadeck 질문에 관련 코드를 추가했습니다. 내가 명시 적으로 그 중 하나를 사용하지 않는거야,하지만 난 항상'obj_get'을 사용하고 싶습니다. – surjikal

답변

6

나는 다음과 같은 리소스 방법의 조합

를 사용하여이 작업을 수행 할 수 있었다

예를 들어, 사용자 자원

#Django 
from django.contrib.auth.models import User 
from django.conf.urls import url 

#Tasty 
from tastypie.resources import ModelResource 

class UserResource(ModelResource): 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'users' 

     #Disallow list operations 
     list_allowed_methods = [] 
     detail_allowed_methods = ['get', 'put', 'patch'] 

     #Exclude some fields 
     excludes = ('first_name', 'is_active', 'is_staff', 'is_superuser', 'last_name', 'password',) 

    #Apply filter for the requesting user 
    def apply_authorization_limits(self, request, object_list): 
     return object_list.filter(pk=request.user.pk) 

    #Override urls such that GET:users/ is actually the user detail endpoint 
    def override_urls(self): 
     return [ 
      url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"), 
     ] 

자원의 세부 사항을 얻기를위한 기본 키 이외의 사용은 지금까지해야합니까 무엇 Tastypie Cookbook

+6

'apply_authorization_limits'는 더 이상 사용되지 않습니다. [참조] (https://github.com/toastdriven/django-tastypie/issues/809) – w43L

+0

그렇다면 이후 버전에서도 어떻게 동일한 결과를 얻을 수 있습니까? – Gabriel

관련 문제