我有一个需要列表作为参数的类。问题是当我导入此函数并将其用作参数时,我得到TypeError
或ValueError
。
我尝试搜索此问题,但似乎找不到匹配的问题。
出现问题的代码
from payroll.payroll import get_employee_names
class ManagerForm(forms.Form):
names = forms.ChoiceField(choices=[get_employee_names()])
导入的功能:
def get_employee_names():
# place uploaded files into variables
tips_file = 'media/reports/Tips_By_Employee_Report.xls'
# get managers name
df_employee_names = pd.read_excel(
tips_file, sheet_name=0, header=None, skiprows=7)
df_employee_names.rename(
columns={0: 'Employee'}, inplace=True)
df_employee_names['Employee'] = \
df_employee_names['Employee'].str.lower()
# data-frame to list
employee_names = df_employee_names.loc[:, 'Employee'].tolist()
return employee_names
我希望它使用函数结尾处返回的列表来创建下拉菜单。
我当前遇到的主要错误是:ValueError at /select-manager-run-payroll/ too many values to unpack (expected 2)
编辑:来发现它期望一个元组作为参数。
答案 0 :(得分:3)
ChoiceField
choices参数应接收元组列表,而不是列表的1元素列表。
重写get_employee_names
以提供:
def get_employee_names():
# ...
# expecting that employee_names is something like ['a', 'b', 'c']
return [(name, name) for name in employee_names]
以及您的表单:
class ManagerForm(forms.Form):
name = forms.ChoiceField(choices=get_employee_names())