1

I have recently installed autocomplete-light in my app.

Autocomplete filters through the field called 'name' in a table called institution. However, what is post through the view is the 'id' of the same object, not the name.

Does anyone know why that is?

My view is:

class UserAccountsUpdate(UpdateView):
    context_object_name = 'variable_used_in `add_user_accounts.html`'
    form_class = AddUserAccountsForm
    template_name = 'add_user_accounts.html'
    success_url = 'add_user_accounts.html'

    def add_user_institution_details(request):
        if request.method == 'POST':
            # create a form instance and populate it with data from the request:
            form = AddUserAccountsForm(request.POST)
            # check whether it's valid:
            if form.is_valid():
                institution_selected = Institution.objects.get(id=name)
                form.save()

        return render(request)

    #get object
    def get_object(self, queryset=None): 
        return self.request.user

The form is:

class AddUserAccountsForm(forms.ModelForm):

    name = forms.ModelChoiceField(required=True, queryset=Institution.objects.all(), widget=autocomplete_light.ChoiceWidget('InstitutionAutocomplete'), label="")

    class Meta:
        model = Institution
        fields = ('name',)
H C
  • 1,138
  • 4
  • 21
  • 39

1 Answers1

0

autocomplete-light's ChoiceWidget uses the Model's PrimaryKey for post requests by default, which in your case is id.

Since you did not post your models.py I can only assume that name is a CharField in the Institution model and you are just using autocomplete here to simplify the adding of a name.

To realize this use TextWidget and forms.CharField:

class AddUserAccountsForm(forms.ModelForm):

    name = forms.CharField(
        required=True,
        widget=autocomplete_light.TextWidget('InstitutionAutocomplete'),
        label="",
    )

    class Meta:
        model = Institution
        fields = ('name',)
Leistungsabfall
  • 6,368
  • 7
  • 33
  • 41
  • I see. Thanks very much! that works. Part of the same code. Do you know why institution_selected = Institution.objects.get(name=name) doesn't fetch any results? The entry must be in the table because autocomplete found it. – H C Nov 24 '15 at 15:46
  • Does `name` come from `request.POST` or from `form`? Also do you get the desired output if you add `print(name)` in your code? (look out for space characters) – Leistungsabfall Nov 24 '15 at 17:25
  • First, thanks for you help. I have implement the view function in class format, which is making debugging very hard. My print statement doesn't show up. (Is there a better way to debug in class format?) I am "assuming" name comes from when a user select a name from the autocomplete. – H C Nov 24 '15 at 17:45
  • Is there any output when you put `print('name: ' + form.cleaned_data['name'])` after calling `form.is_valid()` ? – Leistungsabfall Nov 24 '15 at 19:54
  • The print doesn't show up in my terminal. That's why I'm having such a hard time debugging this error. If you can suggest where I can see the output, that would be great. However, as suggested elsewhere on Stack Overflow, I installed Django debug toolbar. When I look at the results of the POST, it gives me the correct 'name' of the institution that I selected from the autocomplete. – H C Nov 24 '15 at 21:28
  • Does it work if you rename the method `add_user_institution_details` to `post`? If it does not you should consider asking a new question and include your URL configuration, form, view and model. – Leistungsabfall Nov 24 '15 at 22:08
  • Thanks, I have posted a separate question here. If you can help, that would be great! http://stackoverflow.com/questions/33910120/different-implementations-for-autocomplete-light – H C Nov 25 '15 at 16:51