Search code examples
pythondjangotastypie

tastypie queryset values is not being displayed


I have this tastypie resource:

class TagResource_min(ModelResource):
     class Meta:
          queryset=Question.objects.values('text', 'id')

When I do this I have the error:

{

    "error_message": "'dict' object has no attribute 'pk'",
    "traceback": "
        Traceback (most recent call last): 

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 219, in wrapper    
        response = callback(request, *args, **kwargs)  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 450, in dispatch_list    
        return self.dispatch('list', request, **kwargs)  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 482, in dispatch    
        response = method(request, **kwargs)  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1340, in get_list    
        for obj in to_be_serialized[self._meta.collection_name]  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1340, in <listcomp>    
        for obj in to_be_serialized[self._meta.collection_name]  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 895, in full_dehydrate    
        data[field_name] = method(bundle)  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1068, in dehydrate_resource_uri    
        return self.get_resource_uri(bundle)  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 819, in get_resource_uri    
        return self._build_reverse_url(url_name, kwargs=self.resource_uri_kwargs(bundle_or_obj))  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 798, in resource_uri_kwargs    
        kwargs.update(self.detail_uri_kwargs(bundle_or_obj))  

        File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 775, in detail_uri_kwargs    
        kwargs[self._meta.detail_uri_name] = getattr(bundle_or_obj, self._meta.detail_uri_name)

        AttributeError: 'dict' object has no attribute 'pk'"

}

The 'dict' it is referring to must be the queryset obtained by Question.objects.values('text', 'id'). When I log into shell and ask for Question.objects.values('text', 'id') I have the following:

<QuerySet [{'text': 'Why does capillary action take place?', 'id': 1}, {'text': "If a human brain was connected to a fish's body, how would the humans thoughts change?", 'id': 2},...

What am I doing wrong by using objects.values()? How do I display a new queryset using objects.values()?

EDIT When I use the Question.objects.values() it returns an array of dict which does not have the attribute dict.pk. But when I do Question.objects.all() it returns an array of ModelResource type, which all return an integer when I call ModelResource.pk.

This information is not really helpful... haha. Because I am sure that the solution lies in the tastypie or django library. Althought, if I can figure out a way to attach a pk on the dict that will solve my problems.


Solution

  • The values() method returns a queryset that returns dictionaries instead of model instances. A model instance has a pk attribute but the dictionary does not. Tastypie is trying to access pk in the dict, thus giving the error. You need to use a method that returns a queryset giving model instances.

    From Django's documentation use values():

    when you know you’re only going to need values from a small number of the available fields and you won’t need the functionality of a model instance object.

    In your case, you do need the functionality of model instance object, pk attribute.

    I'd suggest using Question.objects.all()