Django 中的自动完成过程运行良好。我的问题是,我不仅要过滤模型字段中的第一个字母,还要过滤first_namefirst_name 可能具有的任何字母。例如,假设我first_name在表 Profile的列中存储了以下值:露娜·罗拉琳达·莫拉·罗娜通过键入 L,搜索将返回 Louna、Lola、Linda。我想通过输入na搜索返回 Lou na和 Rou na有任何想法吗?我的课:class ProfileAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): # Don't forget to filter out results depending on the visitor ! # if not self.request.user.is_authenticated(): # return Profile.objects.none() qs = Profile.objects.all() if self.q: qs = qs.filter(first_name__istartswith=self.q) return qs我的型号:class Profile(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) created_date = models.DateTimeField(default=datetime.datetime.now) def __str__(self): return self.first_name+ " " +self.last_name我的表格:from django import forms from .models import Profile from dal import autocomplete from django.forms import ModelChoiceField class ProfileForm(forms.ModelForm): first_name = forms.ModelChoiceField(queryset=Profile.objects.all(),widget=autocomplete.ModelSelect2(url='profile-autocomplete')) class Meta: model = Profile fields = ('__all__')我的模板:{% load static %}{% block content %}<div> <form action="" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" /> </form></div>{% endblock %}{% block footer %}<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>{{ form.media }}{% endblock %}
添加回答
举报
0/150
提交
取消