1

thanks for looking at my problem:

How do I display the text in the form textbox, but retrieve the ID on posting the form.

Everything works as expected with Autocomplete, expect for correctly displaying the reps name.

When I try this below I can post the form, using the PK, but the pk number is displayed rather than the reps name. I want to see the reps name in the text box

view.py

            rep_json = {}  
            rep_json['id'] = item.pk
            rep_json['label'] = f'{item.rep_first_name} {item.rep_last_name}'
            rep_json['value'] = item.pk

I have tried various combinations to get this to work, but when I can display the text in the textbox, the validation fails on the pk.

The field being autocompleted is foreign key, hence the validation failure.

sandbox.html

<script>
$(function() {
  $("#autoc").autocomplete({
    source: "/autocomplete/",
    minLength: 2,
  });
});
</script>

<form method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
</form>

forms.py

class sales_form2(forms.ModelForm):
    class Meta:
        model = sales
        fields = (
            'position',
            'rep',     
            'comm'    )

        widgets = { 'rep':  forms.TextInput(attrs={'style':'width:100px','data-url':'autocomplete/','id':'autoc'})}

views.py - Autocomplete

def sandbox_complete(request):
    if request.is_ajax():
        q = request.GET.get('term', '')
        theReps = reps.objects.filter(Q(rep_first_name__icontains = q)|Q(rep_last_name__icontains=q))

        results = []
        for item in theReps:
            rep_json = {}  
            rep_json['id'] = item.pk
            rep_json['label'] = f'{item.rep_first_name} {item.rep_last_name}'
            rep_json['value'] = item.pk 

            results.append(rep_json)
        data = json.dumps(results)
    else:
        data = 'fail'
    mimetype = 'application/json'
    return HttpResponse(data, mimetype)

Any pointers would be appreciated, even what I could search on next, as I've seem to have exhausted the internet on this one.

Many thanks Graham

Graham Russon
  • 45
  • 1
  • 8

1 Answers1

1

I'm quite new to stackoverflow, but I used an autocomplete in one of my django projects recently.

For clarification if I understood your problem correctly:

You set the value of the TextInput to item.pk so you will see the PK displayed in your TextInput field.

If you would set the value to the name of the rep, your TextInput field would display the correct rep name, but on POST your view would only get the rep name and not the id.

So I would have two suggestions:

  1. You could change the logic of retrieving the rep's. For example to make the name of the rep unique so you can search with the name in your database and get the correct id then (I don't know if that is good practice)
  2. You could maybe use a hidden input field which holds the id
TM.96
  • 167
  • 14
  • Thanks for the response. You have understood the problem correctly. On point 1. Good idea, however the rep is a foreign key, so validation fails. On point 2, I could if I knew how to integrate that with a form. I will convert from a models form to a normal form and try point 1. – Graham Russon Jul 23 '19 at 07:17
  • Oh yeah, I missed the `forms.ModelForm`. Maybe this could help you with the hidden input field in a form in django: [Django ModelForm to have a hidden input](https://stackoverflow.com/questions/15795869/django-modelform-to-have-a-hidden-input). Let me know if it works or you need more suggestions, maybe I can help you further. – TM.96 Jul 24 '19 at 08:37
  • I worked around this by converting to a forms.form, and used your suggestion of the hidden field which did the trick. ```` – Graham Russon Jul 25 '19 at 07:18
  • Glad that I could help! – TM.96 Jul 27 '19 at 11:24