Reputation: 21
class ProjectResource(ModelResource):
class Meta(object):
queryset = models.Projects.objects.all()
resource_name = 'projects'
list_allowed_methods = ['get', 'post', 'put']
always_return_data = True
default_format = 'application/json'
user = auth.ValidateUser()
def get_list(self, request, **kwargs):
user_projects = super(ProjectResource, self).get_list(request, **kwargs)
result = json.dumps(user_projects.content)
return http.HttpResponse(result,
content_type='application/json', status=200)
This is my tastypie resource. If I found a user from auth.ValidateUser() I need to reply only the associate project from projects table. If not I want to display a error dict saying no project is assigned. I'm not a able to add a filter to get_list.
I'm able to do with get_object_list but if I wanted to reply a custom response like error dict ({'error': 'No user mapped.'}) it is throwing error AttributeError: "'dict' object has no attribute 'filter'"
Any help is much appreciated here.
Upvotes: 1
Views: 477
Reputation: 4370
You could do the filtering in get_object_list
, then in get_list
if the queryset is empty return your dict.
Alternatively, in get_object_list
you can do:
from tastypie.exceptions import ImmediateHttpResponse
response = http.HttpResponse(json.dumps({'error': 'No user mapped.'}),
content_type='application/json', status=400)
raise ImmediateHttpResponse(response=response)
Upvotes: 1