How to validate 'instance' in a Django form constructor MyForm(instance=myInstance)
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, 开发者_如何学Gothis 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?Custom form field is what you need - simple, clean, reusable on any form. Probably even custom model field, but that is up to you.
精彩评论