Search code examples
python-3.xdjangoglobal-variables

Python global variable not working in Django


I'm passing kwargs from a function to another function and declare the variable as global to use it in query but not working.

def do_stuff_then_call_taxman(**kwargs):
    employee_id = kwargs['employee_id']

    ''' doing stuff'''

    taxman(employee_id=employee_id)

def taxman(**kwargs):

    global employee_id #<--- ATTENTION HERE 
    employee_id = kwargs['employee_id']



qs = Employee.objects.filter(id=employee_id).values() #Error occurs here

 NameError at /timesheet/ name 'employee_id' is not defined


for global_variable in qs:

    '''variables'''
   

I can't figure out what I'm doing wrong. Help will be appreciated.


Solution

  • you just define employee_id,but it does not exist in the runtime


    add something like do_stuff_then_call_taxman(employee_id=1)beforeqs = Employee.objects.filter(id=employee_id).values()