[Answer]-Return just single object (detail) instead of list

1👍

You should write your own view to wrap around tastypie dispatch_detail :

class UserResource(ModelResource):
  [...]

  def prepend_urls(self):
    return [ url(r'^(?P<resource_name>%s)/$' % self._meta.resource_name, self.wrap_view('current_user'), name='api_current_user') ]

  def current_user(self, request, **kwargs):
    user = getattr(request, 'user', None)
    if user and not user.is_anonymous():
        return self.dispatch_detail(request, pk=str(request.user.id))

0👍

try:

user = get_object_or_404(User, username=username)

0👍

To get the object for the currently logged in user you can pull this from the request object.

user = request.user

or

user = User.Objects.get(username=user.username)

or using the answer from @gyln

user = get_object_or_404(User, username=user.username)

Obviously if you are doing it this way ensure that the user is authenticated at the view.

Using the object.filter function will always return a list even if there is just one result; whereas the objects.get method will only return one result.

Leave a comment