Search code examples
pythondjangodjango-modelsdjango-viewsdjango-filter

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 Logged in User details.

Below is the snapshot of filter.py

class networkFilterUserbased(django_filters.FilterSet):

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

    request = kwargs['request']
    username = request.user.id

    my_choices = NetworkRelatedInformation.objects.values_list('account', 'account__accountName') \
    .filter(account__accountusermapping__userId=username).distinct()

    self.filters['account'].extra.update({'choices': my_choices})

class Meta:
    model = NetworkRelatedInformation
    fields = ['month', 'year', 'account']

And i am using this filter in my views.py

def UserSpecificDashBoardView(request, *args, **kwargs):
    month = request.GET.get('month')
    year = request.GET.get('year')
    account = request.GET.get('account')

   ......All Logic here....



   network_list_user = NetworkRelatedInformation.objects.all()
   network_filter_user = networkFilterUserbased(request.GET, queryset=network_list_user)
   return render(request, 'personal/dashboardnew.html', {'filter': network_filter_user})

Now the problem is when i am passing hardcoded values like username of the user in above example as uername1 it is working fine, But there are 100s of users so i want to pass the value directly from request.

Means when i am passing request.user in place of username it is not working.

I tried multiple solutions using request based filtering but nothing works. Pls advise how can i pass the currently logged in user value to the above filter.

models.py

class AccountInformation(models.Model):
    accountName = models.CharField(max_length=40)
    countryName = models.CharField(max_length=40)
    managerName = models.CharField(max_length=40)
    location = models.CharField(max_length=40)

class AccountUserMapping(models.Model):
    accountId = models.ForeignKey('AccountInformation', on_delete=models.DO_NOTHING)
    userId = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name='%(class)s_requests_created')


class NetworkRelatedInformation(models.Model):
    MONTH_CHOICES = [(str(i), calendar.month_name[i]) for i in range(1, 13)]

    account = models.ForeignKey('AccountInformation', on_delete=models.DO_NOTHING)
    month = models.CharField(max_length=9, choices=MONTH_CHOICES, default='1')
    year = models.IntegerField(default=0)

    alarm_count = models.IntegerField(default=0)
    tt_count = models.IntegerField(default=0)
    cr_count = models.IntegerField(default=0)
    wo_count = models.IntegerField(default=0)
    subs_count = models.IntegerField(default=0)

Problem is in filter.py i am not able to get request.user

Trace back
Traceback (most recent call last):
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\esacjak\Desktop\Code Backup\operatewebportal\personal\views\account.py", line 4917, in UserSpecificDashBoardView
'totalsrfothersheadcount': totalsrfothersheadcount
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\defaulttags.py", line 314, in render
return nodelist.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\widget_tweaks\templatetags\widget_tweaks.py", line 176, in render
bounded_field = self.field.resolve(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 676, in resolve
obj = self.var.resolve(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 802, in resolve
value = self._resolve_lookup(context)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\base.py", line 843, in _resolve_lookup
current = getattr(current, bit)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django_filters\filterset.py", line 231, in form
for name, filter_ in six.iteritems(self.filters)])
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django_filters\filterset.py", line 231, in <listcomp>
for name, filter_ in six.iteritems(self.filters)])
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django_filters\filters.py", line 406, in field
return super(QuerySetRequestMixin, self).field
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django_filters\filters.py", line 199, in field
self._field = self.field_class(label=self.label, **field_kwargs)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django_filters\fields.py", line 237, in __init__
super(ChoiceIteratorMixin, self).__init__(*args, **kwargs)
File "C:\Users\esacjak\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\models.py", line 1173, in __init__
initial=initial, help_text=help_text, **kwargs
TypeError: __init__() got an unexpected keyword argument 'choices'
[11/Jul/2018 22:08:50] "GET /personal/dashboardnew/ HTTP/1.1" 500 301865

Solution

  • 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:

    class networkFilterUserbased(django_filters.FilterSet):
    
        account = django_filters.ChoiceFilter(choices=None)
    
        class Meta:
            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.