2015-02-24 133 views
0

我想使用djangorestframework-csv這樣的:Django的REST框架和CSVRenderer

# serializers.py 
class ResourceSerializer(serializers.ModelSerializer): 
    url = serializers.SerializerMethodField('get_url') 
    file = serializers.SerializerMethodField('`enter code here`get_file') 
    extra_file = serializers.SerializerMethodField('get_extra_file') 
    owner = serializers.SerializerMethodField('get_owner') 

    def get_url(self, resource): 
     return resource.get_absolute_url() 

    def get_file(self, resource): 
     return resource.get_file() 

    def get_extra_file(self, resource): 
     return resource.get_extra_file() 

    def get_owner(self, resource): 
     return resource.owner.email if resource.owner else None 

    class Meta: 
     model = Resource 

     fields = (
      'name', 
      'description', 
      'file', 
      'extra_file', 
      'resource_type', 
      'date_uploaded', 
      'date_recorded', 
      'location', 
      'owner', 
      'url', 
     ) 

# views.py 
@api_view(['GET']) 
@renderer_classes((CSVRenderer,)) 
def resources(request): 
    """ 
    Return all public Resources. 
    """ 

    resources = Resource.objects.filter(status='Public') 

    return Response(
     ResourceSerializer(
      resources, 
      many=True 
     ).data 
    ) 

但服務器返回:

Response Body 
detail 

Could not satisfy the request's Accept header 

Response Code 
406 NOT ACCEPTABLE 

Response Headers 
{ 
    "Date": "Tue, 24 Feb 2015 14:36:38 GMT", 
    "Allow": "OPTIONS, GET", 
    "Server": "WSGIServer/0.1 Python/2.7.3", 
    "Vary": "Cookie", 
    "Content-Type": "text/csv; charset=utf-8" 
}

我會很高興爲這個問題的任何解決方案。

回答