using single view function or class for multiple types of users

27 views
Skip to first unread message

Harsh Gundecha

unread,
Jun 3, 2019, 6:34:53 AM6/3/19
to Django users
what is recommended practice for serving same object to multiple types of users with slight change in output
for e.g book object or issued book object to lets say staff and students, is it a good idea to use same URL for both and serve by checking user type
PS: its a JSON based rest API in rest framework

Thank you
Regards, Harsh

Chetan Ganji

unread,
Jun 4, 2019, 12:02:23 PM6/4/19
to Django users
RE: "slight change in output"
First of all define what it really means. What needs to be changed and how.

RE " is it a good idea to use same URL for both and serve by checking user type"
Yes and No. 

It depends on your mentality. I personally prefer to have separate endpoints for different types of users, 
so if I change something for one user, I dont have to check if its still working for the other user or not.
I would create different apps for different types of users. And each app would contain the endpoints for that particular user type.
This is the best way I know. 

Other way to do this, is to update the core GET, POST, PUT, Patch, etc methods of the django/rest framework and return a diff response by checking the user type.
e.g. If you are overwriting the Update View in django rest framework, you could overwrite the update method and check the user type after perform_update() was called.
And return a different response by checking the user type. It will work.

I hope it helps :) 

Cheers!

Harsh Gundecha

unread,
Jun 6, 2019, 6:39:09 AM6/6/19
to Django users
thank you
this perfectly makes sense
Reply all
Reply to author
Forward
0 new messages