Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Custom decorator to pass custom data to function in Django

I want to create custom decorator that checks current user and run a query for this user to get some data that belongs him and then if has defined data function is called with whole list passed else returns 403 error. How can i approach this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can write decorator like this:

# check_data is what the user must have access to
def custom_decorator(check_data):
    def decorator(func):
        def wrapper(request, *args, **kwargs):
            user = request.request.user
            # data_list is list of his access
            data_list = []
            if user.is_authenticated:
                pass
                # you can do your query here to check access
            
            if check_data in data_list:
                kwargs['access_list'] = data_list
                return func(request, *args, **kwargs)
            
            # return Http Error here

        return wrapper

    return decorator

Add access_list parameter to your view function:

@custom_decorator("something")
def get(self, request, access_list=None):
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!