Shreko
Shreko

Reputation: 333

interlace related fields from 2 forms with crispy-forms

I need to accept dimensions from users where they can change units between inches and mm. From the user's perspective they will see a input field and next to it select dropdown of options in/mm with one being default. In half pseudo code, assuming that there are two forms:

<div class="row">
  <div class="col">{{ form.dim_a }}</div>
  <div class="col">{{ units_form.dim_a }}</div>
</div>
<div class="row">
  <div class="col">{{ form.dim_b }}</div>
  <div class="col">{{ units_form.dim_b }}</div>
</div>

On the other side in database, I think the cleanest design would be with all units fields in JSON Field, so it doesn't litter main table with many fields that are in/mm. Something like (as a dict):

{'dim_a': 2, 'dim_b': 45, 'units': {'dim_a': 'in', 'dim_b': 'mm'}}

My problem is how to design django forms to handle this, preferably using crispy-forms as they are freeing me from messy html.

Upvotes: 0

Views: 13

Answers (0)

Related Questions