0

I have a little Django form that allows me to change the stage of an assessment. This is used by staff to keep the user and other staff members up to date on where they're at.

Currently, I'm just loading the default option as what I have set as default in the model ("NEW"), I would prefer if this would instead select the option that the current model of the detail view page is actually set to, e.g. ("IN PROGRESS"). Screenshot below is what it currently displays.

enter image description here

So if the stage is In Progress then on the page load, I would like this dropdown option to have already selected "In Progress".

Is there any method either within the form widget settings, html template, or within Javascript to make this change?

Forms.py

class ChangeStageForm(ModelForm):
    class Meta:
        model = Assessment
        fields = ['assessment_stage', ]
        labels = {
            'assessment_stage': _('Change Assessment Stage'),
        }

Model Field with Choices

ASSESSMENT_STAGE_CHOICES = [
        ('NEW', 'New'),
        ('INPRO', 'In Progress'),
        ('WAIT', 'Waiting on Applicant'),
        ('CAN', 'Cancelled'),
        ('DEC', 'Declined'),
        ('COMP', 'Completed'),
    ]
assessment_stage = models.CharField(max_length=21, choices=ASSESSMENT_STAGE_CHOICES, default='NEW')

Template HTML

Assessment Stage: {{ assessment.get_assessment_stage_display }}
<form method="POST">
     {% csrf_token %}
     {{ change_stage_form }}
     <button type="submit" name="change_stage" class="btn btn-success">Save</button>
</form>

1 Answers1

0

Found the answer after posting. For anyone else looking for the answer.

When creating a new form instance (in my case within the get_context_data of my detail view class) use the initial parameter:

context['change_stage_form'] = ChangeStageForm(initial={
         'assessment_stage': self.object.assessment_stage
})

Django Forms Docs: https://docs.djangoproject.com/en/dev/ref/forms/api/#initial-form-values

Thank you to: https://stackoverflow.com/a/813474/16395136