![]() ![]() ![]() Note that this allows Frank to manage all users under RainDrops company. Let’s say Frank and Joe belong to a company called RainDrops with roles SYS_ADMIN and SYS_USER respectively. ![]() Okay! But I want to limit the user’s information based on the current session user role. Now, your overridden get_serializer_class will look like, You can introduce a new variable serializer_action_classes along with serializer_class under a viewset which maps the viewset action name to serializer class. Under a viewset you can do this by overriding get_serializer_class method. Now, your application wants to use UserSerializer when the client hits the /users/ API endpoint and UserDetailSerializer for the /users// API endpoint. Here, we created two serializer classes for each model, one for listing purpose and another one for detailed lookup. Now, let’s create a few serializer classes for these models as follows: User model has been extended from Common & AbstractUser models and has a field company refers to Company which means mostly each user (except is_superuser, is_staff members) should belong to a company with at least SYS_USER role. Let’s start with Company and User models defined in the gist as follows: But for a specific user’s detailed view you might want to give other information as well such as addresses, phone numbers, job title, department, record creation/modification time, and record created/modified by whom, etc. Hence the application needs different serializer classes based on the requirement at the time of writing APIs in DRF.įor instance, to get the list of users in pagination style, you may simply be interested in showing their names, email IDs, and the organization they’re working for. Practically and for most cases that I have observed, it so happens that an application tends to expose only required information based on what a client asks (requests) the resource. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |