allisson/django-rest-framework-role-filters

Access instance in get_role_id()?

halfnibble opened this issue · 3 comments

What if I need access to the instance object in order to return a role_id?

Is there a way to get that for update operations?

hey, friend . You use the request.user can get user_id and use the role_id = User.object.get(id=user_id).role it can return you wanna result .
i think this question can use drf to solve ,don't about role_filter.

Yes. I was able to figure it out with DRF request. I just added this method to my viewset.

    def get_role_id(self, request):
        if request.user.is_superuser:
            return CustomRole.Admin
        else:
            return CustomRole.Customer

Forgot one thing. In another case, I needed the actual entity instance.

For that case, I was able to access it with:
self.get_instance() in the get_role_id() method.

And it looks like I created my own get_instance method.

    def get_instance(self) -> Optional[MyModel]:
        if "pk" in self.kwargs:
            pk = self.kwargs.get("pk")
            try:
                return MyModel.objects.get(pk=pk)
            except MyModel.DoesNotExist:
                return None
        return None