Automatically fill in the type field for a new Feeding entry with the value used for the latest entry.

This commit is contained in:
Christopher Charbonneau Wells 2018-03-13 19:49:21 -07:00
parent b10944b030
commit cdaef446a0
1 changed files with 13 additions and 0 deletions

View File

@ -36,6 +36,18 @@ def set_default_duration(kwargs):
return kwargs return kwargs
# Sets the default Feeding type to the one used in the most recent entry.
def set_default_feeding_type(kwargs):
instance = kwargs.get('instance', None)
if not kwargs.get('initial'):
kwargs.update(initial={})
if instance is None and models.Feeding.objects.latest('end'):
kwargs['initial'].update({
'type': models.Feeding.objects.latest('end').type
})
return kwargs
class ChildForm(forms.ModelForm): class ChildForm(forms.ModelForm):
class Meta: class Meta:
model = models.Child model = models.Child
@ -107,6 +119,7 @@ class FeedingForm(forms.ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
kwargs = set_default_child(kwargs) kwargs = set_default_child(kwargs)
kwargs = set_default_feeding_type(kwargs)
self.timer_id = kwargs.get('timer', None) self.timer_id = kwargs.get('timer', None)
kwargs = set_default_duration(kwargs) kwargs = set_default_duration(kwargs)
super(FeedingForm, self).__init__(*args, **kwargs) super(FeedingForm, self).__init__(*args, **kwargs)