Okay, just following up on this, I was able to get this to work by passing the Employee object to the form as an argument. Doing this I was able to establish the Department list and Earnings Type list that correspond to the agency of the user. Using this method, the form shows the correct information in the selection lists.
Here is the code…
class EmployeeEarningsForm(ModelForm):
class Meta:
model = EmployeeEarningsTemplate
fields = ['department', 'earnings_type', 'hours', 'rate']
labels = {
'department': 'Department',
'earnings_type': 'Earnings Type',
'hours': 'Hours',
'rate': 'Rate',
}
def __init__(self, employee, *args, **kwargs):
super(ModelForm, self).__init__(*args, **kwargs)
self.fields['department'] = (
DepartmentModelChoiceField(queryset=Departments.objects.filter(agency=employee.agency),
empty_label='(select department)',
widget=forms.Select(attrs={'style': 'width: 200px;'})
)
)
self.fields['earnings_type'] = (
EarningsTypeModelChoiceField(queryset=EarningsTypes.objects.filter(agency=employee.agency),
empty_label='(select type)',
widget=forms.Select(attrs={'style': 'width: 200px;'})
)
)
class EarningsTypeModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
self.widget_attrs({'style', 'width:200px;'},)
return '%s' % obj.name
class DepartmentModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
self.widget_attrs({'style', 'width:200px;'},)
return '%s' % obj.name
I probably should have mentioned earlier, that from the outset, I have the employee object. This will not be a situation where the user will select the employee and the model-choices will change. I’m getting to the view with the PK of the employee object.
As I mentioned, this does render properly and only shows departments and earnings types that are associated with the Agency that the employee is related to.
Now, the issue I’m having is during the save of the form. This is the view that receives the updated form.
def employee_earnings(request, pk):
employee = Employee.objects.get(pk=pk)
earnings = EmployeeEarningsTemplate.objects.get(employee=employee)
form = EmployeeEarningsForm(employee=employee, instance=earnings)
if request.method == 'POST':
form = EmployeeEarningsForm(request.POST, employee=employee, instance=form.instance)
if form.is_valid():
earnings_entry = form.save(commit=False)
earnings_entry.employee = employee
earnings_entry.save()
return redirect('employee_list')
context = {'employee_id': pk,
'form': form}
return render(request, 'PayrollPersonnel/employee_earnings.html', context)
When I get to the line after if request.method == 'POST'
, I get the following error.
TypeError at /personnel/employee-earnings/83ec3f21-fda3-478e-8d85-af117bdeb5c1/
EmployeeEarningsForm.__init__() got multiple values for argument 'employee'
I’m passing the current employee to the form instantiation, but maybe the employee already exists? However, I’ve also tried removing the employee argument and I get this error…
AttributeError at /personnel/employee-earnings/83ec3f21-fda3-478e-8d85-af117bdeb5c1/
'QueryDict' object has no attribute 'agency'
I’m guessing it can’t find the agency because that comes from the employee, and I am not sending the employee argument in this version.
I’m not sure which method (with or without the employee argument) is correct, but I’m stuck at this point.
Any ideas on what I might be missing?
Thanks!!