61 lines
2.7 KiB
Python
61 lines
2.7 KiB
Python
from django import forms
|
|
from .models import Dinosaur
|
|
|
|
|
|
class DinosaurForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Dinosaur
|
|
fields = [
|
|
'name', 'era', 'diet', 'description', 'image',
|
|
'background', 'speed', 'weight', 'intelligence', 'height',
|
|
'facts',
|
|
]
|
|
widgets = {
|
|
'name': forms.TextInput(attrs={'class': 'input', 'placeholder': 'T. rex'}),
|
|
'era': forms.Select(attrs={'class': 'input'}),
|
|
'diet': forms.Select(attrs={'class': 'input'}),
|
|
'description': forms.Textarea(attrs={'class': 'textarea', 'rows': 3}),
|
|
'image': forms.ClearableFileInput(attrs={'class': 'file-input'}),
|
|
'background': forms.Select(attrs={'class': 'input'}),
|
|
'speed': forms.NumberInput(attrs={'class': 'input', 'min': 0, 'max': int(Dinosaur.SPEED_MAX_KMH), 'placeholder': 'km/h'}),
|
|
'weight': forms.NumberInput(attrs={'class': 'input', 'min': 0, 'max': int(Dinosaur.WEIGHT_MAX_KG), 'placeholder': 'kg'}),
|
|
'intelligence': forms.NumberInput(attrs={'class': 'input', 'min': 0, 'max': 100, 'placeholder': '0-100 score'}),
|
|
'height': forms.NumberInput(attrs={'class': 'input', 'min': 0, 'max': int(Dinosaur.HEIGHT_MAX_M), 'step': '0.1', 'placeholder': 'metres'}),
|
|
}
|
|
|
|
# Add facts field widget
|
|
widgets['facts'] = forms.Textarea(attrs={'class': 'textarea', 'rows': 3, 'placeholder': 'One fact per line'})
|
|
|
|
|
|
from .models import BackgroundImage
|
|
|
|
|
|
class BackgroundImageForm(forms.ModelForm):
|
|
class Meta:
|
|
model = BackgroundImage
|
|
fields = ['title', 'image', 'is_active']
|
|
widgets = {
|
|
'title': forms.TextInput(attrs={'class': 'input', 'placeholder': 'Rocky desert'}),
|
|
'image': forms.ClearableFileInput(attrs={'class': 'file-input'}),
|
|
'is_active': forms.CheckboxInput(attrs={'class': 'checkbox'}),
|
|
}
|
|
|
|
|
|
from django.forms import modelformset_factory
|
|
|
|
# A simplified form used in the bulk edit table (only a few editable fields)
|
|
class DinosaurBulkForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Dinosaur
|
|
fields = ['name', 'background', 'speed', 'weight', 'height', 'intelligence']
|
|
widgets = {
|
|
'name': forms.TextInput(attrs={'class': 'input'}),
|
|
'background': forms.Select(attrs={'class': 'input'}),
|
|
'speed': forms.NumberInput(attrs={'class': 'input', 'min': 0}),
|
|
'weight': forms.NumberInput(attrs={'class': 'input', 'min': 0}),
|
|
'height': forms.NumberInput(attrs={'class': 'input', 'step': '0.1'}),
|
|
'intelligence': forms.NumberInput(attrs={'class': 'input', 'min': 0, 'max': 100}),
|
|
}
|
|
|
|
DinosaurBulkFormSet = modelformset_factory(Dinosaur, form=DinosaurBulkForm, extra=0)
|