Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use a custom Authorization class that blocks the list endpoints and gracefully raises an error instead of just removing the URL all together so it still plays nicely with Tastypie.</p> <pre><code>class UserObjectsOnlyAuthorization(Authorization): def read_list(self, object_list, bundle): raise Unauthorized("Sorry, no list reads.") def read_detail(self, object_list, bundle): # Is the requested object the user? return bundle.obj == bundle.request.user def create_list(self, object_list, bundle): raise Unauthorized("Sorry, no creates.") def create_detail(self, object_list, bundle): raise Unauthorized("Sorry, no creates.") def update_list(self, object_list, bundle): raise Unauthorized("Sorry, no updates.") def update_detail(self, object_list, bundle): raise Unauthorized("Sorry, no updates.") def delete_list(self, object_list, bundle): # Sorry user, no deletes for you! raise Unauthorized("Sorry, no deletes.") def delete_detail(self, object_list, bundle): raise Unauthorized("Sorry, no deletes.") </code></pre> <p>EDIT:</p> <p>If you'd like to force this API always to be a 'Detail' request then you can override Tastypie's built in functions. Basically if you specify an ID in the URL then tastypie routes it to be a _detail request and if you don't then it routes it to be a _list request. If you override the dispatch functions which detect this, you can change all requests to this resource to be _detail and specify what the primary key is to look up your user. This may be a bit more hacky, but will accomplish what you want:</p> <pre><code>def dispatch(self, request_type, request, **kwargs): # Force this to be a single User object return super(UserResource, self).dispatch('detail', request, **kwargs) def get_detail(self, request, **kwargs): # Place the authenticated user's id in the get detail request kwargs['id'] = request.user.pk return super(UserResource, self).get_detail(request, **kwargs) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload