Overwrite clean method in Django Custom Forms

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-03-03T14:03:47Z Indexed on 2010/03/22 0:01 UTC
Read the original article Hit count: 759

Hi I have wrote a custom widget

class AutoCompleteWidget(widgets.TextInput):
"""
widget to show an autocomplete box which returns a list on nodes available to be tagged
"""
def render(self, name, value, attrs=None):
    final_attrs = self.build_attrs(attrs, name=name)

    if not self.attrs.has_key('id'):
        final_attrs['id'] = 'id_%s' % name

    if not value: value = '[]'

    jquery = u"""
    <script type="text/javascript">
    $("#%s").tokenInput('%s', {
        hintText: "Enter the word",
        noResultsText: "No results",
        prePopulate: %s,
        searchingText: "Searching..."
    });

    $("body").focus();
    </script>
    """ % (final_attrs['id'], reverse('ajax_autocomplete'), value)

    output = super(AutoTagWidget, self).render(name, "", attrs)

    return output + mark_safe(jquery)

class MyForm(forms.Form):
    AutoComplete = forms.CharField(widget=AutoCompleteWidget)

this widget uses a jquery function which autocompletes a word based on entries from the database. You can preset its initial values by setting prePopulate to a json string in the form

['name': 'some name', 'id': 'some id']

I do this by setting the inital value of the form field to this json string

jquery_string = ['name': 'some name', 'id': 'some id']
form = MyForm(initial={'AutoComplete':jquery_string})

When submitting the form the the value of AutoComplete is returned as a comma seperated list of the selected ids e.g. 12,45,43,66 which if what I want.

However if there is an error in the form, for example a required field has not been entered the value of the AutoComplete field is now 12,45,43,66 and not the json string which it requires.

What is the best way to solve this. I was thinking about overwriting the clean method in the form class but I'm not sure how to find out if any other element has returned an error. e.g.

if forms.errors
   form.cleaned_date['autocomplete'] = json string

return form.cleaned_data

Thanks

© Stack Overflow or respective owner

Related posts about django

Related posts about django-forms