From 8f3c90c11aa45958dd8760d5a819e0dd075ffa0b Mon Sep 17 00:00:00 2001 From: Andrey Smakotin Date: Sat, 10 Jan 2026 11:45:25 +0300 Subject: [PATCH] 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 --- myproject/inventory/forms.py | 15 ++++++++------- myproject/inventory/views/writeoff_document.py | 7 +++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/myproject/inventory/forms.py b/myproject/inventory/forms.py index 7d2af7e..4e04fbe 100644 --- a/myproject/inventory/forms.py +++ b/myproject/inventory/forms.py @@ -243,18 +243,16 @@ class WriteOffDocumentForm(forms.ModelForm): fields = ['warehouse', 'date', 'notes'] widgets = { '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': 'Примечания к документу'}), } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['warehouse'].queryset = Warehouse.objects.filter(is_active=True) - - # Устанавливаем дату по умолчанию - сегодня - if not self.initial.get('date'): - from django.utils import timezone - self.initial['date'] = timezone.now().date() + + # Отключаем локализацию для поля date, чтобы использовать формат yyyy-MM-dd + self.fields['date'].input_formats = ['%Y-%m-%d'] # Если есть склад по умолчанию - предвыбираем его if not self.initial.get('warehouse'): @@ -346,7 +344,7 @@ class IncomingDocumentForm(forms.ModelForm): fields = ['warehouse', 'date', 'receipt_type', 'supplier_name', 'notes'] widgets = { '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'}), 'supplier_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Наименование поставщика'}), 'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3, 'placeholder': 'Примечания к документу'}), @@ -355,6 +353,9 @@ class IncomingDocumentForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) 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'): diff --git a/myproject/inventory/views/writeoff_document.py b/myproject/inventory/views/writeoff_document.py index 0e1886b..3247410 100644 --- a/myproject/inventory/views/writeoff_document.py +++ b/myproject/inventory/views/writeoff_document.py @@ -35,6 +35,13 @@ class WriteOffDocumentCreateView(LoginRequiredMixin, CreateView): form_class = WriteOffDocumentForm 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): document = WriteOffDocumentService.create_document( warehouse=form.cleaned_data['warehouse'],