Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access self.request.user #103

Open
caclement opened this issue Mar 6, 2024 · 2 comments
Open

Access self.request.user #103

caclement opened this issue Mar 6, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@caclement
Copy link

caclement commented Mar 6, 2024

In the documentation it seems you managed to refer to self.request.user when creating customs filters.

https://django-slick-reporting.readthedocs.io/en/latest/topics/filter_form.html

    def get_filters(self):
        # return the filters to be used in the report
        # Note: the use of Q filters and kwargs filters
        filters = {}
        q_filters = []
        if self.cleaned_data["secure"] == "secure":
            filters["is_secure"] = True
        elif self.cleaned_data["secure"] == "non-secure":
            filters["is_secure"] = False
        if self.cleaned_data["method"]:
            filters["method"] = self.cleaned_data["method"]
        if self.cleaned_data["response"]:
            filters["response"] = self.cleaned_data["response"]
        if self.cleaned_data["other_people_only"]:
            q_filters.append(~Q(user=self.request.user))

However when I do it, I get an error :

    def get_filters(self):
        filters = {}
        q_filters = []
        filters["location_id"] = self.cleaned_data["location_id"]
        filters["test"] = self.request.user.email
        return q_filters, filters

The error is : AttributeError: 'ManagerForm' object has no attribute 'request'

@caclement caclement changed the title Access Access self.request.user Mar 6, 2024
@RamezIssac
Copy link
Owner

Hello, Thank you for this.
Yes the documentation is missing that

  1. the form is accepting the request on its init, and attaching it to self
  2. How the view sends the request to the form
class MyReport(ReportView):
    # ....       
    def get_form_kwargs(self):
          kwargs = super().get_form_kwargs()
          kwargs["request"] = self.request # send the request as a form kwarg, as you'd do in any CBV
          return kwargs


class RequestLogForm(BaseReportForm, forms.Form):
    # ... 
    def __init__(self, request, *args, **kwargs):
        # special init signature to accept the request and attach it
        self.request = request 
        # ....
        

@RamezIssac RamezIssac added documentation Improvements or additions to documentation enhancement New feature or request labels Mar 7, 2024
@caclement
Copy link
Author

Thanks ! It works perfectly. Awsome application by the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants