Skip to content

Add Custom Fields to Scenario Forms

Ryan Hodges edited this page Dec 16, 2017 · 1 revision

Intro

To include custom fields to your scenario form, the workflow looks something like this:

  • Create your custom Model in your app's models.py by inheriting the Scenario model.
  • Create your custom form in Forms.py by inheriting from the ScenarioForm form.
  • Following How To Add Scenarios to a MarinePlanner Project update your settings to point GET_SCENARIOS_URL, SCENARIO_FORM_URL, and/or SCENARIOS_LINK_BASE appropriately.

Creating Your Custom ScenarioModel in Models.py

** Todo **

Creating Your Custom ScenarioForm in Forms.py

Create your Form:

from scenarios.forms import ScenarioForm
class CustomScenarioForm(ScenarioForm):
   ...

Add Your Fields

Note that 'description' and 'name' are included in the existing model and are applied to your form at the end.

For each field someone may filter/query on, you will need to create that field here. Keep in mind the helpful, pre-build widgets available to support this in both madrona-analysistools and in madrona-scenarios' own widgets file.

5 different types of form-fields can be combined for building your form to create a single field on the screen - which ones are required depends on your widget:

  • field name:
    • this is a boolean. It tracks whether or not to consider filtering/querying on this field
    • Most common widget it django.forms.widgets.CheckboxInput
  • field min:
    • This field allows you to filter out any numeric data less-than this value
    • On its own works with a regular analysistools.widgets.SliderWidget or a scenarios.widgets.SliderWidgetWithToolTip
    • When coupled with field max look at DualSliderWidget (analysistools) or DualSliderWidgetWithTooltip(

For now, see the EFH code for a good example.

Group Your Fields Into 'Steps'

There is limited real-estate for a massive form, but you may want to allow a great deal of filter/query options when crafting scenarios. To do this, you will want to break you form into 'steps' or 'sub-forms' that will be shown on the screen one-at-a-time.

The process works like this:

def get_step_0_fields(self):
    names = (
        (field_1, field_1_min, field_1_max, field_1_input, field_1_checkboxes),
        (....)
    )

    return self._get_fields(names)

def get_step_1_fields(self):
    ...

def get_steps(self):
   return self.get_step_1_fields(), self.get_step_1_fields(),...