Getting objects in Autocomplete Field?

I have a working autocomplete field that displays my users, to select them. I cannot get the name that is selected from this field. I need it in order to pass the name in a view function to later filter for the id.

How would I get the name in my POST request?

commit=false doesn’t seem to allow me to do this; I am confused about why because I have done it that way in the past. Thank you for any creative ideas here.

 <input name="user" id="user" type="text" list='results' placeholder="Type Customer's name" class="form-control">
                     <datalist id="results">
                                {% for x in results %}
                                    <option value="{{x.first_name}} {{x.last_name}}"></option>
                                    {% endfor %}
                      </datalist>

Use the network tab in your browser’s developer tools to see what is being submitted for that field - that will give you the information you need. You could also add a print statement in your view to see how the view receives it.

Found it!

My Post Request is storing the first name that is selected. Now I pass this name into my views. My issue is if I try to put:

{{x.first_name}} {{last.name}}

I could .split()[1] these in my view and just search by the last name. But if a user has the same last name as another I am in trouble.

Any ideas about how to filter for the user_id, so I don’t run into an issue where I am getting the wrong id?

My models where set up like this:

from django.db import models
from django.contrib.auth.models import AbstractUser
#from localflavor.us.models import USStateField


# Create your models here.
class User(AbstractUser):
    """User can be Employee or Customer"""
    #state = USStateField()

class Business(models.Model):
    business = models.CharField(max_length=50)

class BusinessOwner(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True )
    business = models.ForeignKey(Business, on_delete=models.CASCADE, null=True)

class Customer(models.Model):
    """ Customer-specific information """
    JOB_STATUS =(
        ("Q", "Quote"),
        ("R", "Review"),
        ("W", "Working"),
        ("C", "Complete"),
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True )
    business = models.ForeignKey(Business, on_delete=models.CASCADE, null=True)
    cust_notes = models.CharField(max_length=1000, blank=True, null=True)
    job_status = models.CharField(max_length=10, blank=True,null=True, choices = JOB_STATUS ) #add.update(review = 'R') to change field.
    job_name = models.CharField(max_length=500, blank=True, null=True)
    note_by = models.CharField(max_length=500, blank=True, null=True)
    note_created_at = models.DateTimeField(auto_now_add=True)
    note_updated_at = models.DateTimeField(auto_now=True, null=True)
    selected_cust = models.CharField(max_length=50, blank=True )


class Employee(models.Model):
    """ Employee-specific information """
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    business = models.ForeignKey(Business,  on_delete=models.CASCADE, null=True, blank=True)

part of my views.py that is applicable

def customer_note(request):
      if request.method =="POST":
            customer_form = CustomerForm(request.POST)
            if customer_form.is_valid():
                  get_id = customer_form.save(commit=False) # getting first and last name here to do something with it. The Input in my first post is in a Form. and CustomerForm is that form.
                  full_name = User.objects.filter( Q(id__icontains=get_id))
                  customer_form.instance.id =  full_name.id #This is where I am stuck.