Files
django-cresus/core/forms.py
2017-09-21 13:12:35 +02:00

26 lines
881 B
Python

from django import forms
from .models import Enregistrement
class EnregistrementForm(forms.ModelForm):
est_negatif = forms.BooleanField(required=False)
class Meta:
model = Enregistrement
fields = ('montant', 'description', 'date', 'etiquette')
widgets = {'etiquette': forms.Select(
attrs={'class':'form-control custom-select'}
),
'date': forms.SelectDateWidget(
attrs={'class':'form-control custom-select mx-1'}
)
}
def clean(self):
data = super().clean()
print(data)
# Force un nombre négatif si 'est_negatif' est coché
if (data['est_negatif']
and data['montant'] > 0):
self.cleaned_data['montant'] = 0 - data['montant']
return self.cleaned_data