views:

22

answers:

1

I am making a form in Django. The field to display is a numeric field representing some limit. It's possible for there to be no limit. Rather than force the user to enter some strange number to mean unlimited (e.g. -1), I'd like there to be a radio button, with 2 options: "Unlimited" and the second option being a text box that the user can enter the value into.

The original model only has the IntegerField which can be null. A null value is used to mean unlimited. Is there some way I can make a FormField of this field that will include this checkbox functionality?

I know I can hand write the HTML myself, I'm wondering if there is a already made django field/widget that'll do this.

A: 

You can either make the form have an additional form field for the checkbox. And override it's save method to fill the model fields accordingly. Or you can make a custom form field widget that would hold both HTML input fields and produce the appropriate python value from the inputs and vice versa. The first option being the simpler to implement, and wiser if you need this functionality only in one form. Either way, I'd reconsider using a null value to mean anything except no value entered. You could make it so that the Integer field holds -1 to mean unlimited if all other user specified values can only be positive. Or add another boolean model field to hold the value for this checkbox.

Vasil