Fix date field with explicit input_formats and format in UPSHostForm

This commit is contained in:
xiaji
2026-04-30 15:09:22 +08:00
parent 749ab06ee7
commit 03190ae977
2 changed files with 22 additions and 21 deletions

View File

@@ -4,6 +4,13 @@ from .models import UPSHost, Battery
class UPSHostForm(forms.ModelForm):
last_maintenance_date = forms.DateField(
required=False,
label='上次维保时间',
input_formats=['%Y-%m-%d'],
widget=forms.DateInput(attrs={'class': 'form-control datepicker', 'placeholder': '选择日期'}, format='%Y-%m-%d'),
)
class Meta:
model = UPSHost
fields = ['brand', 'model', 'ip_address', 'quantity', 'location', 'last_maintenance_date', 'contact']
@@ -13,7 +20,6 @@ class UPSHostForm(forms.ModelForm):
'ip_address': 'IP地址',
'quantity': '数量',
'location': '存放位置',
'last_maintenance_date': '上次维保时间',
'contact': '联系人',
}
widgets = {
@@ -22,7 +28,6 @@ class UPSHostForm(forms.ModelForm):
'ip_address': forms.TextInput(attrs={'class': 'form-control'}),
'quantity': forms.NumberInput(attrs={'class': 'form-control', 'min': '1'}),
'location': forms.TextInput(attrs={'class': 'form-control'}),
'last_maintenance_date': forms.DateInput(attrs={'class': 'form-control datepicker', 'type': 'text'}),
'contact': forms.Select(attrs={'class': 'form-control'}),
}

View File

@@ -119,14 +119,13 @@ class UPSHostUpdateView(UpdateView):
def form_valid(self, form):
self.object = form.save()
if self.request.POST:
battery_formset = BatteryFormSet(self.request.POST, instance=self.object)
if battery_formset.is_valid():
battery_formset.save()
context = self.get_context_data()
battery_formset = context['battery_formset']
if battery_formset.is_valid():
battery_formset.instance = self.object
battery_formset.save()
messages.success(self.request, 'UPS主机和电池信息已更新')
return super().form_valid(form)
return redirect(self.success_url)
class UPSHostDeleteView(DeleteView):
@@ -255,7 +254,7 @@ class SupplierDeleteView(DeleteView):
success_url = reverse_lazy('supplier_list')
class MaintenanceRecordListView(ListView):
class MaintenanceListView(ListView):
model = MaintenanceRecord
template_name = 'ups_manager/maintenance_list.html'
context_object_name = 'maintenance_list'
@@ -263,35 +262,32 @@ class MaintenanceRecordListView(ListView):
def get_queryset(self):
queryset = super().get_queryset()
ups_model = self.request.GET.get('ups_model')
ups_host = self.request.GET.get('ups_host')
technician = self.request.GET.get('technician')
supplier = self.request.GET.get('supplier')
if ups_model:
queryset = queryset.filter(ups_host__model__icontains=ups_model)
if ups_host:
queryset = queryset.filter(ups_host__model__icontains=ups_host)
if technician:
queryset = queryset.filter(technician__icontains=technician)
if supplier:
queryset = queryset.filter(supplier__company_name__icontains=supplier)
return queryset.order_by('-maintenance_date')
class MaintenanceRecordCreateView(CreateView):
class MaintenanceCreateView(CreateView):
model = MaintenanceRecord
template_name = 'ups_manager/maintenance_form.html'
fields = ['ups_host', 'battery', 'supplier', 'maintenance_date', 'content', 'technician']
fields = ['ups_host', 'battery', 'supplier', 'maintenance_date', 'technician', 'content']
success_url = reverse_lazy('maintenance_list')
class MaintenanceRecordUpdateView(UpdateView):
class MaintenanceUpdateView(UpdateView):
model = MaintenanceRecord
template_name = 'ups_manager/maintenance_form.html'
fields = ['ups_host', 'battery', 'supplier', 'maintenance_date', 'content', 'technician']
fields = ['ups_host', 'battery', 'supplier', 'maintenance_date', 'technician', 'content']
success_url = reverse_lazy('maintenance_list')
class MaintenanceRecordDeleteView(DeleteView):
class MaintenanceDeleteView(DeleteView):
model = MaintenanceRecord
template_name = 'ups_manager/maintenance_confirm_delete.html'
success_url = reverse_lazy('maintenance_list')