fix(inventory): enforce consistent date format in document forms and views

- Add explicit date input format '%Y-%m-%d' to WriteOffDocumentForm and IncomingDocumentForm
- Disable localization for date fields to ensure yyyy-MM-dd format is used
- Set initial date value to current date in WriteOffDocumentCreateView's get_initial method
- Restrict warehouse queryset to active warehouses in forms initialization
- Improve date widget consistency by adding format parameter to DateInput widgets
This commit is contained in:
2026-01-10 11:45:25 +03:00
parent 5f565555e3
commit 8f3c90c11a
2 changed files with 15 additions and 7 deletions

View File

@@ -243,7 +243,7 @@ class WriteOffDocumentForm(forms.ModelForm):
fields = ['warehouse', 'date', 'notes'] fields = ['warehouse', 'date', 'notes']
widgets = { widgets = {
'warehouse': forms.Select(attrs={'class': 'form-control'}), 'warehouse': forms.Select(attrs={'class': 'form-control'}),
'date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}), 'date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}, format='%Y-%m-%d'),
'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3, 'placeholder': 'Примечания к документу'}), 'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3, 'placeholder': 'Примечания к документу'}),
} }
@@ -251,10 +251,8 @@ class WriteOffDocumentForm(forms.ModelForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields['warehouse'].queryset = Warehouse.objects.filter(is_active=True) self.fields['warehouse'].queryset = Warehouse.objects.filter(is_active=True)
# Устанавливаем дату по умолчанию - сегодня # Отключаем локализацию для поля date, чтобы использовать формат yyyy-MM-dd
if not self.initial.get('date'): self.fields['date'].input_formats = ['%Y-%m-%d']
from django.utils import timezone
self.initial['date'] = timezone.now().date()
# Если есть склад по умолчанию - предвыбираем его # Если есть склад по умолчанию - предвыбираем его
if not self.initial.get('warehouse'): if not self.initial.get('warehouse'):
@@ -346,7 +344,7 @@ class IncomingDocumentForm(forms.ModelForm):
fields = ['warehouse', 'date', 'receipt_type', 'supplier_name', 'notes'] fields = ['warehouse', 'date', 'receipt_type', 'supplier_name', 'notes']
widgets = { widgets = {
'warehouse': forms.Select(attrs={'class': 'form-control'}), 'warehouse': forms.Select(attrs={'class': 'form-control'}),
'date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}), 'date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}, format='%Y-%m-%d'),
'receipt_type': forms.Select(attrs={'class': 'form-control'}), 'receipt_type': forms.Select(attrs={'class': 'form-control'}),
'supplier_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Наименование поставщика'}), 'supplier_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Наименование поставщика'}),
'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3, 'placeholder': 'Примечания к документу'}), 'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3, 'placeholder': 'Примечания к документу'}),
@@ -356,6 +354,9 @@ class IncomingDocumentForm(forms.ModelForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields['warehouse'].queryset = Warehouse.objects.filter(is_active=True) self.fields['warehouse'].queryset = Warehouse.objects.filter(is_active=True)
# Отключаем локализацию для поля date, чтобы использовать формат yyyy-MM-dd
self.fields['date'].input_formats = ['%Y-%m-%d']
# Если есть склад по умолчанию - предвыбираем его # Если есть склад по умолчанию - предвыбираем его
if not self.initial.get('warehouse'): if not self.initial.get('warehouse'):
default_warehouse = Warehouse.objects.filter( default_warehouse = Warehouse.objects.filter(

View File

@@ -35,6 +35,13 @@ class WriteOffDocumentCreateView(LoginRequiredMixin, CreateView):
form_class = WriteOffDocumentForm form_class = WriteOffDocumentForm
template_name = 'inventory/writeoff_document/form.html' template_name = 'inventory/writeoff_document/form.html'
def get_initial(self):
"""Устанавливаем начальные значения для формы"""
initial = super().get_initial()
from django.utils import timezone
initial['date'] = timezone.now().date()
return initial
def form_valid(self, form): def form_valid(self, form):
document = WriteOffDocumentService.create_document( document = WriteOffDocumentService.create_document(
warehouse=form.cleaned_data['warehouse'], warehouse=form.cleaned_data['warehouse'],