Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper implementation of 'obj_get' in Django Tastypie?

I'm quite new to Django & Tastypie. I would like to return only one of the objects from the query. I've tried almost everything and cannot seem to find the solution. Here is my code below:

class ProfileResource(ModelResource):
     person = fields.ForeignKey(UserResource, 'user', full=True)

class Meta:
    queryset = Person.objects.all()
    resource_name = 'profile'
    authentication = BasicAuthentication()
    authorization = DjangoAuthorization()
    serializer = Serializer(formats=['json'])

Now the part I'm having trouble with is how can I return a single user object from a single resource using request.user.

like image 342
deadlock Avatar asked Oct 20 '12 16:10

deadlock


1 Answers

If you only want to show one resource I would probably create new resource view (named as my_profile) that would call normal detail view with user in kwargs and removed other urls:

from django.conf.urls import url
from tastypie.utils import trailing_slash
class ProfileResource(ModelResource):
    ...
    def base_urls(self):
        return [
            url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_my_profile'), name="api_dispatch_my_profile")
        ]

    def dispatch_my_profile(self, request, **kwargs):
        kwargs['user'] = request.user
        return super(ProfileResource, self).dispatch_detail(request, **kwargs)
like image 112
rychlis Avatar answered Sep 17 '22 18:09

rychlis