Skip to content Skip to sidebar Skip to footer

How To Pass Currently Logged In User To Filter.py I.e Request Based Filtering In Django

I want to restrict the views for each user i.e a user can view only those account details that are related to him only. For this i have created a Filter where i want to pass the Lo

Solution 1:

How to get Current Logged In user in Filters.py file Django

In my case I wanted Team Lead, based on currently logged In user(manager). For that I had Views.py file and filters.py file.

filters.py file code:

class FeedbackSearchFilterManager(django_filters.FilterSet):

task__team_lead = django_filters.ModelChoiceFilter(name='task__team_lead', label='Team Lead', empty_label="---select expert----",
                                          widget=forms.Select(attrs={'class': 'form-control'}), queryset=None)

classMeta:
    model = Feedback
    fields = ['search', 'status',]

# this method for getting current logged in user in filter.py filedef__init__(self, *args, **kwargs):
    super(FeedbackSearchFilterManager,self).__init__(*args, **kwargs)
    request = kwargs['request']
    if request.user.is_authenticated:
        username = request.user.id
        userlocation = request.user.location_id
        self.filters['task__team_lead'].queryset = UserAccount.objects.filter(manager_id=username, location=userlocation)

In this image you can see I imported def__init__()

In that file def init() method will return you the current logged in user. As you can see in my task__team_lead modelchoice filter I passed one attribute

name"task__team_lead" >> this is a foreign key relation bcz No team_lead model filed in Feedback model but in Task Model we have team_lead field and both Task and Feedback table are connected with foriegn key.

And I am getting the list of team lead(based on manager and manager location and team lead location is same) when I logged In as manager.

Solution 2:

Have you checked the constructor of FilterSet? It looks like this:

def__init__(self, data=None, queryset=None, prefix=None, strict=None, request=None):
   pass

So I believe, if you properly instantiate your filter, you can access current request in your filter and you should be able to access request.user.

network_filter_user = networkFilterUserbased(queryset=network_list_user, request=request)   

update

AFter you change the thing i mentioned. you can update the choices for your filter in init method of your filter:

classnetworkFilterUserbased(django_filters.FilterSet):

    account = django_filters.ChoiceFilter(choices=None)

    classMeta:
        model = ** model **
        fields = ['account', ]

    def__init__(self, *args, **kwargs):
        super().__init__(*args,**kwargs)

        request = kwargs['request']          
        if request.user.is_authenticated:
            username = request.user.username
            my_choices = ** build your choices here**  
            self.filters['account'].extra.update( { 'choices' : my_choices })

Note: I could not verify your query to get list of choices you want as i dont have the same DB. So That's up to you to have it correct.

Post a Comment for "How To Pass Currently Logged In User To Filter.py I.e Request Based Filtering In Django"