feat: add PlantForm and PruningLogForm
This commit is contained in:
parent
62a0d32619
commit
b3f0a1cd40
1 changed files with 47 additions and 0 deletions
47
plants/forms.py
Normal file
47
plants/forms.py
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
from django import forms
|
||||||
|
from .models import Plant, PruningLog
|
||||||
|
|
||||||
|
MONTH_CHOICES = [
|
||||||
|
('1', 'Jan'), ('2', 'Feb'), ('3', 'Mar'), ('4', 'Apr'),
|
||||||
|
('5', 'May'), ('6', 'Jun'), ('7', 'Jul'), ('8', 'Aug'),
|
||||||
|
('9', 'Sep'), ('10', 'Oct'), ('11', 'Nov'), ('12', 'Dec'),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class PlantForm(forms.ModelForm):
|
||||||
|
pruning_months = forms.MultipleChoiceField(
|
||||||
|
choices=MONTH_CHOICES,
|
||||||
|
widget=forms.CheckboxSelectMultiple,
|
||||||
|
required=False,
|
||||||
|
label='Pruning months',
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Plant
|
||||||
|
fields = ['name', 'location', 'is_indoor', 'pruning_months', 'photo', 'notes']
|
||||||
|
widgets = {
|
||||||
|
'notes': forms.Textarea(attrs={'rows': 3}),
|
||||||
|
'name': forms.TextInput(attrs={'class': 'form-control'}),
|
||||||
|
'location': forms.TextInput(attrs={'class': 'form-control'}),
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
# JSONField stores ints; CheckboxSelectMultiple expects strings matching choice values
|
||||||
|
if self.instance.pk and self.instance.pruning_months:
|
||||||
|
self.initial['pruning_months'] = [str(m) for m in self.instance.pruning_months]
|
||||||
|
elif not self.instance.pk and self.initial.get('pruning_months'):
|
||||||
|
self.initial['pruning_months'] = [str(m) for m in self.initial['pruning_months']]
|
||||||
|
|
||||||
|
def clean_pruning_months(self):
|
||||||
|
return [int(m) for m in self.cleaned_data.get('pruning_months', [])]
|
||||||
|
|
||||||
|
|
||||||
|
class PruningLogForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = PruningLog
|
||||||
|
fields = ['pruned_on', 'notes']
|
||||||
|
widgets = {
|
||||||
|
'pruned_on': forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}),
|
||||||
|
'notes': forms.Textarea(attrs={'rows': 2, 'class': 'form-control'}),
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue