Django Autocomplete Light-“无法加载结果" [英] Django Autocomplete Light - "The results could not be loaded"
问题描述
我在一个小型应用程序上使用Django-autocomplete-light.用户界面似乎正常工作,我可以从一些可见的值中进行选择.当我在框中键入一个或多个字母时,就会出现问题.通常,它应该过滤/选择结果,相反,我收到错误消息无法加载结果"(参见图片).
I'm using Django-autocomplete-light on a small application. The UI seems to work, and I can select from some visible values. The problem arises when I type one or more letters into the box. Normally it should filter/select the results, instead, I get the error "The results could not be loaded" (see picture).
似乎jquery工作正常,除了我在框中键入内容时不进行过滤.很高兴添加更多代码,只是不确定我需要添加什么.
Seems like the jquery is working fine, except for not filtering when I type in the box. Happy to add more code, just not sure what I need to add yet.
models.py
models.py
class Encounter(models.Model):
patid = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, verbose_name=('Patient Name'), related_name='patient')
created_by = models.ForeignKey(Users, editable=False, null=True, blank=True, on_delete=models.PROTECT, related_name='encounter_created_by')
encounter_date = models.DateField()
encounter_label = models.ForeignKey(EncounterReason, on_delete=models.PROTECT, verbose_name=('Encounter Reason'), related_name='fk_reason')
class EncounterReason(models.Model):
reason = models.CharField(max_length=256, blank=True, null=True)
valueset_id = models.CharField(max_length=256, blank=True, null=True)
views.py
views.py
class EncounterReasonAutocomplete(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 EncounterReason.objects.none()
qs = EncounterReason.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
forms.py
forms.py
class EncounterForm(forms.ModelForm):
encounter_date = forms.DateField(initial=datetime.date.today, widget = DateInput())
encounter_notes = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'Encounter Notes', 'id': 'editor', 'rows':50, 'cols':25}))
encounter_label = forms.ModelChoiceField(queryset=EncounterReason.objects.all(),
widget=autocomplete.ModelSelect2(url='encounterreason-autocomplete')
)
class Meta:
model = Encounter
fields = ('__all__')
urls.py
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('', include('clinicalviewer.urls')),
path('encounterreason-autocomplete/', views.EncounterReasonAutocomplete.as_view(),
name='encounterreason-autocomplete'),
]
无需在框中输入任何内容:
Without typing anything into the box:
现在,当我在框中输入内容时(有些对象以"r"开头):
Now when I type something into the box (there are objects that start with "r"):
错误: 在控制台中,出现以下错误: jquery.js:9203 GET http://127.0.0.1:8000/encounterreason-autocomplete/?q = r 500(内部服务器错误)
Errors: In the console I get the following error: jquery.js:9203 GET http://127.0.0.1:8000/encounterreason-autocomplete/?q=r 500 (Internal Server Error)
推荐答案
EncounterReason
没有字段name
,因此以下过滤器将错误
EncounterReason
does not have a field name
so the following filter will error
if self.q:
qs = qs.filter(name__istartswith=self.q)
您可能希望在reason
字段上进行过滤
You probably want to filter on the reason
field
if self.q:
qs = qs.filter(reason__icontains=self.q)
这篇关于Django Autocomplete Light-“无法加载结果"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!