0

I'm using an autocomplete within a form. The forms save doesn't seem to want to properly take in my hidden field.

The autocomplete shows a name: for instance 'Steve Butabi'. Its supposed to be sending a username. The username is 'stevebutabi'. Upon inspecting the input value. The username 'stevebutabi' properly shows. But within the POST, manager : 'Steve Butabi' shows. And I get a Exception Type: DoesNotExist. I need the hidden username, not the string it's presenting. I know whats happening, I just don't know how to fix it.

So I guess what I'm asking: is there a way to ignore the clean function within a Django form?

forms.py

manager = forms.CharField(max_length=200, required=True, widget=forms.TextInput(attrs={'class':'input-text','id':'id_managerbox'}))

def clean(self):
     if not 'manager' in self.cleaned_data:
          raise forms.ValidationError('You must supply a manager for this fund.')
     return self.cleaned_data

def save(self, request):
     if self.is_valid():
          if self.cleaned_data['manager']:
               manager = ManagerProfile.objects.get(user__username=self.cleaned_data['manager'])
               # I've also tried :
               manager = ManagerProfile.objects.get(user__username=request.POST['manager'])
    ...
               ManagesFund.objects.get(manager=manager).save()

Javascript

   $(document).ready(function() {  
         $.get('/autocomplete/managers/', function(data) {
              var completions = new Array();
              var dict = JSON.parse(data, function(key, value) {
                   completions.push(key);
                   return value;
              });
              $('#id_managerbox').autocomplete({
                   source: completions,
                   minLength: 2,
                   select: function(event, ui) {
                        $('#id_manager').val(dict[ui.item.value]);
                   }
              });
         });
    });

Template

<input type="hidden" name="manager" id="id_manager" />
{{ form.manager }}

Thanks in advance.

rantanplan
  • 7,283
  • 1
  • 24
  • 45
Modelesq
  • 5,192
  • 20
  • 61
  • 88
  • Are you handling the "select" event for your autocomplete? You need to capture the "select" event and then assign the value to the hidden field. – dannyroa May 25 '12 at 21:15
  • @dannyroa Yes. (edit: added js ) – Modelesq May 25 '12 at 21:19
  • What does the json response from '/autocomplete/managers/' look like? – dannyroa May 25 '12 at 22:20
  • What exactly are you asking? `manager : 'Steve Butabi'` is presumably the `repr` or `unicode` of the Manager object identified by that username. So what's wrong with that, precisely? – Daniel Roseman May 25 '12 at 22:50
  • Figured it out. I took `{{ form.manager }}` out and replaced it with ``. No idea why this would matter. – Modelesq May 29 '12 at 19:48

0 Answers0