1

I am trying to use Django ajax-selects package I have my form.py :

from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultipleField
from ajax_select import make_ajax_field
class LeaseTenantForm(forms.ModelForm):
    class Meta:
        model = LeaseTenant
        exclude = []
    tenant  = make_ajax_field(LeaseTenant,'tenant','tenant',help_text="Search for label by name") 
    #tenant = AutoCompleteSelectField('tenant', required=False, help_text=None)

I have my lookup.py:

from ajax_select import register, LookupChannel
from client.models import Tenant

@register('tenant')
class TenantLookup(LookupChannel):

    model = Tenant

    def get_query(self, q, request):
        return self.model.objects.filter(last_name=q)

    def format_item_display(self, item):
        return u"<span class='tag'>%s</span>" % item.last_name

I have my tenant model.py:

class Tenant(CommonInfo):
    version = IntegerVersionField( )

    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField(null=True, blank=True)
    phone = models.CharField(max_length=30)
    language = models.CharField(max_length=1, default='E',
                              choices=LANGUAGE_CHOICES)
    external_address = models.CharField(max_length=90,null=True, blank=True)
    external_zip_code = models.CharField(max_length=50,null=True, blank=True)
    external_city = models.CharField(max_length=60,null=True, blank=True)
    external_state_province = models.CharField(max_length=30,null=True, blank=True)
    external_country = models.CharField(max_length=30,null=True, blank=True)
    status = models.CharField(max_length=1, default='N',
                              choices=TENANT_STATUS_CHOICES,null=True, blank=True)
    def __unicode__(self):
        return u'%s %i %s %s %s %s %s %s' % ("#", self.id,"first_name", self.first_name, "last_name",  self.last_name, "phone", self.phone )

But in my form I am not getting no autocomplete and no field create. What have I missed?

Ilya Bibik
  • 3,924
  • 4
  • 23
  • 48
  • I'm guessing it's the fact that your filter in get_query appears to be using a field that doesn't exist in the Tenant class. If that's not it some diags (e.g. requests/responses) from when you load the form and enter some text would help. – Peter Brittain Nov 12 '16 at 23:24
  • You right the field name was wrong . I copied the wrong version. But this is not the problem. – Ilya Bibik Nov 12 '16 at 23:30
  • Now your filter is an exact match... Don't you want some other possible options (and so want to use contains or startswith)? – Peter Brittain Nov 12 '16 at 23:45
  • the problem is that nothing happens when I input any values and I dont see any error. – Ilya Bibik Nov 12 '16 at 23:51
  • Which is all very plausible when you are looking for an exact match - I would expect no results until you have typed in exactly the right name. – Peter Brittain Nov 13 '16 at 09:09
  • This might be a silly question but you did add the ajax_select_urls in your project's urls.py, right? – Vlad B Nov 14 '16 at 14:37
  • @Vlad B No question is silly:). Yes I did – Ilya Bibik Nov 15 '16 at 12:45
  • you probably shouldn't be using an exact match in an autocomplete lookup it's kind of counterintuitive. You should be using something like an __icontains at the bare minimum – Daniel Petrikin Nov 15 '16 at 18:05

1 Answers1

1

The problem is you're using an exact match query. Autocomplete behavior is inexact. You have:

def get_query(self, q, request):
    return self.model.objects.filter(last_name=q)

You're probably intending something to the effect of:

def get_query(self, q, request):
    return self.model.objects.filter(last_name__icontains=q)