Skip to content

Instantly share code, notes, and snippets.

@namsral
Created February 1, 2012 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namsral/1716333 to your computer and use it in GitHub Desktop.
Save namsral/1716333 to your computer and use it in GitHub Desktop.
Mixin for the Djando Rest Framework (DFR) to filter objects by authenticated user.
class AuthenticatedUserResourceMixin(ResourceMixin):
permissions = (IsAuthenticated,)
def build_query(self, *args, **kwargs):
tmp = dict(kwargs)
if BaseRenderer._FORMAT_QUERY_PARAM in tmp:
del tmp[BaseRenderer._FORMAT_QUERY_PARAM]
if args:
tmp.update({'pk': args[-1]})
# Return objects from authenticated users only.
user = self.request.user if self.request.user.is_authenticated() else 0
tmp.update({'user':user})
return Q(**tmp)
class InstanceUserMessageView(AuthenticatedUserResourceMixin, InstanceModelView):
resource = UserMessageResource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment