forms.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from django import forms
  2. from .models import Journee, Repas, CompositionRepas, Aliment
  3. class JourneeForm(forms.ModelForm):
  4. class Meta:
  5. model = Journee
  6. fields = ['type_journee', 'notes']
  7. widgets = {
  8. 'type_journee': forms.Select(attrs={'class': 'form-select'}),
  9. 'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
  10. }
  11. labels = {
  12. 'type_journee': 'Type de journée',
  13. 'notes': 'Notes',
  14. }
  15. class RepasForm(forms.ModelForm):
  16. class Meta:
  17. model = Repas
  18. fields = ['type_repas', 'heure', 'notes']
  19. widgets = {
  20. 'type_repas': forms.Select(attrs={'class': 'form-select'}),
  21. 'heure': forms.TimeInput(attrs={'class': 'form-control', 'type': 'time'}),
  22. 'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
  23. }
  24. labels = {
  25. 'type_repas': 'Type de repas',
  26. 'heure': 'Heure',
  27. 'notes': 'Notes',
  28. }
  29. class CompositionRepasForm(forms.ModelForm):
  30. class Meta:
  31. model = CompositionRepas
  32. fields = ['aliment', 'nombre_portions', 'notes']
  33. widgets = {
  34. 'aliment': forms.Select(attrs={'class': 'form-select'}),
  35. 'nombre_portions': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.5', 'min': '0.5'}),
  36. 'notes': forms.TextInput(attrs={'class': 'form-control'}),
  37. }
  38. labels = {
  39. 'aliment': 'Aliment',
  40. 'nombre_portions': 'Nombre de portions',
  41. 'notes': 'Notes (préparation, remarques)',
  42. }
  43. def __init__(self, *args, **kwargs):
  44. super().__init__(*args, **kwargs)
  45. # Organiser les aliments par catégorie dans le select
  46. self.fields['aliment'].queryset = Aliment.objects.all().order_by('categorie', 'nom')