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:
@@ -243,18 +243,16 @@ 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': 'Примечания к документу'}),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
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': 'Примечания к документу'}),
|
||||||
@@ -355,6 +353,9 @@ class IncomingDocumentForm(forms.ModelForm):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
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'):
|
||||||
|
|||||||
@@ -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'],
|
||||||
|
|||||||
Reference in New Issue
Block a user