Как передать текущего зарегистрированного пользователя в filter.py, т.е. запрос на основе фильтрации в django - PullRequest
0 голосов
/ 06 июля 2018

Я хочу ограничить просмотры для каждого пользователя, т. Е. Пользователь может просматривать только те данные учетной записи, которые связаны только с ним.

Для этого я создал Фильтр, в который я хочу передать данные Зарегистрированного пользователя.

Ниже приведен снимок файла 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']

И я использую этот фильтр в моих 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})

Теперь проблема в том, что когда я передаю жестко закодированные значения, такие как имя пользователя пользователя в приведенном выше примере, как uername1, он работает нормально, но есть сотни пользователей, поэтому я хочу передать значение непосредственно из запроса.

Означает, что когда я передаю request.user вместо имени пользователя, он не работает.

Я пробовал несколько решений с использованием фильтрации на основе запросов, но ничего не работает. Просьба сообщить, как я могу передать текущее зарегистрированное пользовательское значение вышеупомянутому фильтру.

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)

Проблема в файле filter.py Я не могу получить 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

1 Ответ

0 голосов
/ 06 июля 2018

Вы проверили конструктор FilterSet? Это выглядит так:

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

Так что я считаю, что если вы правильно создадите экземпляр своего фильтра, вы сможете получить доступ к текущему запросу в своем фильтре, и вы сможете получить доступ к request.user.

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

обновление

После того, как вы измените то, что я упомянул. Вы можете обновить выбор для вашего фильтра в методе init вашего фильтра:

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 })

Примечание: я не смог подтвердить ваш запрос, чтобы получить список вариантов, которые вы хотите, так как у меня нет той же БД. Так что это зависит от вас, чтобы это было правильно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...