Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current User in django-filter Method

Tags:

python

django

I have a filter where I need to access the request.user. However, django-filter does not pass it. Without using the messy inspect.stack() is there a way to get the current user in the method member_filter below?

class ClubFilter(django_filters.FilterSet):
    member = django_filters.MethodFilter(action='member_filter')

    class Meta:
        model = Club
        fields = ['member']

    def member_filter(self, queryset, value):
        # get current user here so I can filter on it.
        return queryset.filter(user=???)

For example this works but feels wrong...

def member_filter(self, queryset, value):
        import inspect
        request_user = None
        for frame_record in inspect.stack():
            if frame_record[3] == 'get_response':
                request_user = frame_record[0].f_locals['request'].user
                print(request_user)

is there maybe a way to add this to some middleware that injects user into all methods? Or is there a better way?

like image 291
Prometheus Avatar asked Sep 17 '15 09:09

Prometheus


People also ask

What is filter API in Django?

API Guide 1 DjangoFilterBackend. The django-filter library includes a DjangoFilterBackend class which supports highly customizable field filtering for REST framework. 2 SearchFilter. The SearchFilter class supports simple single query parameter based searching, and is based on the Django admin's search functionality. 3 OrderingFilter. ...

What is the best alternative to filters in Django?

The djangorestframework-word-filter developed as alternative to filters.SearchFilter which will search full word in text, or exact match. django-url-filter provides a safe way to filter data via human-friendly URLs.

What is a searchfilter in Django?

The SearchFilter class supports simple single query parameter based searching, and is based on the Django admin's search functionality. When in use, the browsable API will include a SearchFilter control: The SearchFilter class will only be applied if the view has a search_fields attribute set.

How do I assign a user to a company in Django?

Upon Company creation, whether it’s a functional view or a CBV, you’ll have access to the request in the view processing the company’s creation. From there, you can assign the company’s user. If you’re using a CBV, you can override the form_valid function and assign the user from self.request.user. Here’s the Django documentation on CBVs.


2 Answers

Yes, you can do it, and it's very easy.

First, define __init__ method in your ClubFilter class that will take one extra argument:

class ClubFilter(django_filters.FilterSet):
    # ...

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(ClubFilter, self).__init__(*args, **kwargs)

With having your user saved into attribute inside ClubFilter, you can use it in your filter. Just remember to pass current user from your view inside FilterSet.

like image 71
GwynBleidD Avatar answered Oct 04 '22 07:10

GwynBleidD


Try self.request.user.

Why it must work.

like image 33
lampslave Avatar answered Oct 04 '22 05:10

lampslave