[Fixed]-How does viewset aligns with rest methods

1👍

1/ In general, POST is for creating new, PUT is for updating. See the docs on the SimpleRouter to show how the various types of Http methods align with various actions in your Django backend.

2/ You’ll find that different situations call for different routing methods. If yours is standard, you may want to use a SimpleRouter like the example above. In that case, creating a new user would be a POST request to /user/ and updating a user would be a PUT request to /user/{{user_id}}/.

3/ To limit access to various API methods, you want to use Permissions. It’s possible that you could use one of DRF’s Custom Permissions. I’ve found that in general, if you want only the user to access his/her own profile, it’s easier to either use conditional logic within a view (i.e., return a DRF PermissionDenied exception in the view logic if the request.user.pk is not the pk of that REST url. More often than not, I’ve used the UserPassesTestMixin from Django Braces, that works pretty well to filter user permissions.

4/ I guess the Django Braces mixin above answers this question. You should include a test_func method if you’re using Django Braces that returns True if you want to grant the user access, and False otherwise.

Hope this helps! I agree that these are difficult parts of DRF and perhaps some of these could more effectively get incorporated into the source. Good luck!

Leave a comment