6

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.

1 Answer 1

4

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)
1
  • OMG. Thank you so much. I have been looking for this for hours.
    – Max
    Dec 22, 2013 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.