Django Autocomplete Light - “The results could not be loaded”

喜夏-厌秋 提交于 2020-07-09 14:01:04

问题


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).

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

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

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

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

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:

Now when I type something into the box (there are objects that start with "r"):

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)


回答1:


EncounterReason does not have a field name so the following filter will error

if self.q:
    qs = qs.filter(name__istartswith=self.q)

You probably want to filter on the reason field

if self.q:
    qs = qs.filter(reason__icontains=self.q)


来源:https://stackoverflow.com/questions/57345956/django-autocomplete-light-the-results-could-not-be-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!