我是 Django 和 Python 的新手,所以请耐心等待。我正在尝试创建一个表格时间表形式,如下所示。
时间表表格:
预期结果
我想要实现的是,当我调用特定的时间表时,我应该获得所有相关的时间表详细信息数据,并填写上面的表单,使得与workdate / project的组合相关的所有小时数都在同一行中相关的小时数被放置在相应的工作代码下(即在pic row1中有8个小时' Norm&#39 ;, row2将在RnR下有12个小时)
问题
现在,如何根据工作日/项目组合的工作代码,设置初始值以在每行中填充正确的小时单元格(与模型字段无关),具体时间取决于工作日/项目组合的工作代码?
其次,如果同一个工作日/项目的小时数(例如4小时和4小时),我该如何处理?我怎么把它放在同一行?如果我可以使用字典填充这将很容易?
由于我在10/80/20 ...字段的表单中没有任何初始数据,因此我无法检查是否删除了任何小时数,以便我也可以删除该记录。
我已开始质疑自己如果我的内联林方法在这种情况下甚至有效吗?
因此,底线是如何在此表单中填充这些不相关的表单字段?
我愿意接受任何方法或想法,以使其发挥作用并感谢您的帮助。
如果您发现我做错了什么,请随时纠正我,以便我可以学习。
代码
Models.py
class Workcode(models.Model):
workcode = models.PositiveSmallIntegerField()
workcode_description = models.CharField(max_length=20)
workcode_short = models.CharField(max_length=4)
workcode_serial = models.SmallIntegerField()
class Project(models.Model):
project = models.CharField(max_length=10)
class Employee(models.Model):
name = models.CharField(max_length=10)
class WeekendDate(models.Model):
weekenddate = models.DateField(unique=True)
class Timesheet(models.Model):
employee = models.ForeignKey(Employee)
weekenddate = models.ForeignKey(WeekendDate)
class Meta:
unique_together = ('employee', 'weekenddate')
class TimesheetDetail(models.Model):
timesheet = models.ForeignKey(Timesheet)
workcode = models.ForeignKey(Workcode)
project = models.ForeignKey(Project)
workdate = models.DateField()
hours = models.PositiveSmallIntegerField()
class Meta:
unique_together = ('timesheet','workcode','project','workdate')
forms.py
class TimesheetForm(forms.ModelForm):
class Meta:
model = models.Timesheet
fields = "__all__"
class BaseTimesheetInlineFormSet(BaseInlineFormSet):
def add_fields(self, form, index):
super(BaseTimesheetInlineFormSet, self).add_fields(form, index)
form.fields['10'] = forms.IntegerField(max_value=16, min_value=0, label='Norm', required=False)
form.fields['80'] = forms.IntegerField(max_value=16, min_value=0, label='RnR', required=False)
form.fields['20'] = forms.IntegerField(max_value=16, min_value=0, label='Annu', required=False)
form.fields['21'] = forms.IntegerField(max_value=16, min_value=0, label='Sick', required=False)
form.fields['25'] = forms.IntegerField(max_value=16, min_value=0, label="Carer", required=False)
form.fields['26'] = forms.IntegerField(max_value=16, min_value=0, label='LSL', required=False)
form.fields['13'] = forms.IntegerField(max_value=16, min_value=0, label='Night', required=False)
form.fields['14'] = forms.IntegerField(max_value=16, min_value=0, label="Noon", required=False)
form.fields['71'] = forms.IntegerField(max_value=16, min_value=0, label='Bus', required=False)
form.fields['22'] = forms.IntegerField(max_value=16, min_value=0, label='WComp', required=False)
TimesheetDetailInlineFormset = inlineformset_factory(models.Timesheet,
models.TimesheetDetail,
form = TimesheetForm,
formset=BaseTimesheetInlineFormSet,
# fields=('workdate','project'),
fields='__all__',
extra=7, max_num=9)
views.py
def manage_timesheet1(request, timesheet_id):
"""Edit children and their addresses for a single parent."""
initial_data = [{'workcode': 1, 'project':1, 'workdate': '2017-12-29', 'hours':10, },
{'workcode': 1, 'project':1, 'workdate': '2017-12-30', 'hours':8, }]
timesheet = Timesheet.objects.get(pk=timesheet_id)
if request.method == 'POST':
formset = TimesheetDetailInlineFormset(request.POST, request.FILES, instance=timesheet)
if formset.is_valid():
# This development will be in a saperate branch called 'alternet-time-save'#
converted_data = []
wc_list = ['10','80','20,','21','25','26','13','14','71','22']
# with transaction.atomic():
for form in formset:
if form.has_changed():
obj = form.save(commit=False)
obj.timesheet = timesheet
obj.workcode = Workcode.objects.get(id=5)
for k,v in form.cleaned_data.items():
if k in wc_list and k in form.changed_data:
if v is not None:
obj, created = TimesheetDetail.objects.update_or_create(
# pk = form.cleaned_data['id'].id,
timesheet = timesheet,
workcode = Workcode.objects.get(workcode=k),
project = form.cleaned_data['project'],
workdate = form.cleaned_data['workdate'],
defaults = {
'timesheet' : timesheet,
'workcode' : Workcode.objects.get(workcode=k),
'project' : form.cleaned_data['project'],
'workdate' : form.cleaned_data['workdate'],
'hours' : v
})
return redirect('timesheet:timesheetlist')
else:
print(formset.errors)
else:
# formset = TimesheetDetailInlineFormset(instance=timesheet, initial=initial_data)
formset = TimesheetDetailInlineFormset(instance=timesheet)
if request.is_ajax():
return render(request, 'timesheet/_ajax_manage_timesheet.html', {
'children_formset':formset, 'timesheetid':timesheet_id})
else:
return render(request, 'timesheet/manage_timesheet.html', {
'children_formset':formset, 'timesheetid':timesheet_id})