我正在尝试使用Django ajax-chosen包 我有我的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)
我有我的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
我的租户是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 )
但在我的表单中,我没有得到自动完成功能,也没有创建任何字段。 我错过了什么?
答案 0 :(得分:1)
问题是您使用完全匹配查询。自动完成行为不准确。你有:
def get_query(self, q, request):
return self.model.objects.filter(last_name=q)
您可能打算采取以下措施:
def get_query(self, q, request):
return self.model.objects.filter(last_name__icontains=q)