diff --git a/atlas/forms.py b/atlas/forms.py index 57a453c4..e516468e 100755 --- a/atlas/forms.py +++ b/atlas/forms.py @@ -298,6 +298,22 @@ class NormalCaseForm(ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + # If editing an existing NormalCase, prepopulate age_value/age_unit + try: + instance = kwargs.get('instance') + except Exception: + instance = None + if instance is None: + # also support self.instance when ModelForm sets it + instance = getattr(self, 'instance', None) + if instance is not None and getattr(instance, 'age_days', None) is not None: + try: + val, unit = NormalCase.days_to_value_unit(instance.age_days) + if val is not None and unit is not None: + self.fields['age_value'].initial = val + self.fields['age_unit'].initial = unit + except Exception: + pass self.helper = FormHelper() # Do not render a