Python 全局变量在 Django 中不起作用

Python global variable not working in Django

我将 kwargs 从一个函数传递到另一个函数并将该变量声明为全局变量以在查询中使用它但不起作用。

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'''
   

我不知道我做错了什么。帮助将不胜感激。

你只是定义了employee_id,但是在runtime

中不存在

qs = Employee.objects.filter(id=employee_id).values()

之前添加类似do_stuff_then_call_taxman(employee_id=1)的内容