views:

106

answers:

1

I have the data stored in a format different from the display format.
I've already worked out the form-to-db conversion in the clean_weight() method of the form, this gives me access to properly format data before saving it.

Now I pretend to manipulate the instances weight before the form is displayed but I fail to see a good place to do this inside the form code.

I have 2 alternatives that I know:

  • Create the constructor and take care of that conversion there. It doesn't look good to me, because once you define the constructor it is for all the possible constructor calls(not only for the instance= one) and being only for 1 or 2 fields of the form I'd prefer another solution.
  • Not doing it in the form code, putting it in every view I use, doesn't look comfortable, cause again I need to do it in several places and I'd need to remove the clean_weight from the form to keep it consistent, so I'd need to put both manipulations in every view I use the form.

For the moment of those 2 the one that looks better is the constructor one, but stinks to me.
Any hint?

+1  A: 

Custom form field is what you need - simple, clean, reusable on any form. Probably even custom model field, but that is up to you.

Dmitry Shevchenko
Yep. Create something like WeightField. Put all the funky conversion in there once, and never think about it again. If it's the first time making a custom field it might seem daunting. *Just do it.* :) You can even do things like add parameters for the unit you want to field to appear in to really give you a clean, useful, reusable tool that'll be a pleasure to use vis-a-vis scattering conversions in your code.
Koobz