1👍
✅
Well, I don’t know if this qualifies as elegant, but how about something like this. Here I’m storing the search string as a property on the widget, and setting the property from the POST data in the form constructor. I’m also using name=”foo_picker_text” to indicate the search input for the corresponding input named “foo”.
class AjaxPickerWidget(HiddenInput):
search_text_suffix = "_picker_text"
def __init__(self, **kwargs):
super(AjaxPickerWidget, self).__init__(**kwargs)
self.search_text = None
def render(self, name, value, attrs=None):
start = '<div name="%s" class="ajax_picker">' % (name) + \
'<input class="search_box" name="%s%s" ' % (name, self.search_text_suffix)+ \
'type="text" value="%s" />' % (self.search_text or "")
end = super(AjaxPickerWidget, self).render(name, value, attrs=attrs) + '</div>'
return '%s%s' % (start, end)
class MyForm(Form):
requestor = CharField(widget = AjaxPickerWidget())
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
# find picker text in POST data and tell the corresponding widget about it
for name in self.data:
if name.endswith(AjaxPickerWidget.search_text_suffix):
field_name = name.split(AjaxPickerWidget.search_text_suffix)[0]
self.fields[field_name].widget.search_text = self.data[name]
You might have to tweak it a bit to get attributes to show up where you want them, etc. but hopefully this basically does what you want?
0👍
You could instead of calling is_valid()
on your form create a custom function
from django import forms
class AjaxPickerWidget(forms.TextInput):
def render(self, name, value, attrs=None):
return super(AjaxPickerWidget, self).render(name, value, attrs=attrs)
def validate(self):
values = self.cleaned_data.items():
# Modify the values to fit the original form (delete, change etc)
return whateversleft.is_valid() # Perhaps with some additional checks
Source:stackexchange.com