37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
from django import forms
|
||
from .models import Auto
|
||
|
||
|
||
class AutoForm(forms.ModelForm):
|
||
class Meta:
|
||
model = Auto
|
||
fields = ['brand', 'model', 'year', 'color']
|
||
|
||
# Настройка виджетов и стилей (опционально, но улучшает внешний вид)
|
||
widgets = {
|
||
'brand': forms.TextInput(attrs={
|
||
'class': 'form-control',
|
||
'placeholder': 'Например: Toyota, BMW, Lada'
|
||
}),
|
||
'model': forms.TextInput(attrs={
|
||
'class': 'form-control',
|
||
'placeholder': 'Например: Camry, X5, Granta'
|
||
}),
|
||
'year': forms.NumberInput(attrs={
|
||
'class': 'form-control',
|
||
'placeholder': 'Год выпуска (например, 2023)',
|
||
'min': 1900,
|
||
'max': 2100,
|
||
}),
|
||
'color': forms.TextInput(attrs={
|
||
'class': 'form-control',
|
||
'placeholder': 'Например: чёрный, серебристый, красный'
|
||
}),
|
||
}
|
||
|
||
# Дополнительная валидация (опционально)
|
||
def clean_year(self):
|
||
year = self.cleaned_data.get('year')
|
||
if year and (year < 1900 or year > 2100):
|
||
raise forms.ValidationError("Год выпуска должен быть в разумных пределах (1900–2100)")
|
||
return year |