Integer Form with Select Widget?

28 views
Skip to first unread message

Farhan Khan

unread,
Oct 25, 2014, 12:29:45 PM10/25/14
to django...@googlegroups.com
Hi all,

I want to get an Integer value from the user through a forms.Form. I have three unique requirements for it.
  1. The corresponding Widget is Select or SelectMultiple (either one)
  2. The 'choices' field is dynamic, not static or hard-coded -they are adjusted upon each display of the Form based on program logic.
  3. I want the max_value and min_value of IntegerField to also be dynamic - also adjusted based on program logic.

What I have thus far is this:

class deleteUserForm(forms.Form):
   delUserId = forms.CharField(label='delUserId', widget=forms.Select)

How would this work?

Thanks
- Farhan

Farhan Khan

unread,
Oct 25, 2014, 8:41:50 PM10/25/14
to django...@googlegroups.com
Any takers? I suspect that I need to mess around with the form during instantiation, but not sure how.

Collin Anderson

unread,
Oct 27, 2014, 8:38:42 AM10/27/14
to django...@googlegroups.com
Hi Farhan,

I can think of two ways. Either, like you said during form instantiation, do something like this:

class DeleteUserForm(forms.Form):
   
def __init__(self, data=None, choices=None, more_info=None):
       
super(DeleteUserForm, self).__init__(data)
        widget
= forms.MultipleSelect if more_info else forms.Select
       
self.fields['del_user_id'] = forms.CharField(label='User', widget=widget, choices=choices)
       
self.fields['something_else'] = forms.IntegerField(min_value=something, max_value=something_else)

Or, in your view:

def myview(self):
    choices
= something
    widget
= something
    min_value
= etc
   
class DeleteUserForm(forms.Form):
        del_user_id
= forms.CharField(label='User', widget=widget, choices=choices)
        something_else
= forms.IntegerField(min_value=something, max_value=something_else)
    form
= DeleteUserForm()
   
if request.method ==  'POST':
        form
= DeleteUserForm(request.POST)
   
# etc

Collin
Reply all
Reply to author
Forward
0 new messages