Django-autocomplete-light - "No results found" 启动时在浏览器中 - 在管理员中进行一次搜索后 - 再次在浏览器中找到结果

Django-autocomplete-light - "No results found" in browser on startup - after doing one search in admin - results are found in browser again

我对 Django-autocomplete-light 有一个特殊的问题。当我转到浏览器并尝试搜索某些内容时,我得到“未找到结果”——但是当我转到管理面板并添加模型时,结果会按预期弹出。完成一次后,我可以返回到浏览器,然后结果就会按预期显示。我怀疑存在一些缓存问题,但不确定如何调试它。也许有人可以看看我的设置是否有问题。

models.py

class DogBreeds(models.Model):
    name = models.CharField(max_length=150)

    def __str__(self):
        return self.name


class Advertisement(SoftDeleteModel, TimeStampedModel):
    breed = models.ForeignKey(DogBreeds, on_delete=models.CASCADE, null=True, verbose_name='Hundras')
    

views.py

class BreedAutocomplete(autocomplete.Select2QuerySetView):

    def get_queryset(self):

        if not self.request.user.is_authenticated:
            return DogBreeds.objects.none()

        qs = DogBreeds.objects.all()

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

        return qs

表单模板

{% extends '_base.html' %}

{% load static %}

{% block content %}
    
  <form method="post" enctype="multipart/form-data" id="adForm" data-municipalities-url="{% url 'ajax_load_municipalities' %}" data-areas-url="{% url 'ajax_load_areas' %}" novalidate>
    {% csrf_token %}  

  
    <table>  
      {{ form.as_p }}
    </table>

    <button type="submit">Publish</button>

  </form>

</body>
{% endblock content %}

{% block footer %}

  {% comment %} Imports for managing Django Autocomplete Light in form {% endcomment %}
  <script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
  <link rel="stylesheet" type="text/css" href="{% static 'autocomplete_light/select2.css' %}" />
  <script type="text/javascript" src="{% static 'autocomplete_light/select2.js' %}"></script>
  <link rel="stylesheet" type="text/css" href="{% static 'autocomplete_light/vendor/select2/dist/css/select2.css' %}" />
  <script type="text/javascript" src="{% static 'autocomplete_light/vendor/select2/dist/js/select2.full.js' %}"></script>
  <script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
  {{ form.media }}

  

{% endblock footer %}

Admin.py

class AdvertisementAdmin(admin.ModelAdmin):
    form = NewAdTakeMyDogForm
    readonly_fields = ('id',)

admin.site.register(Advertisement, AdvertisementAdmin)

根据好人 Iain Shelvington 的建议,通过删除以下行解决了该问题!

如果不是self.request.user.is_authenticated: return DogBreeds.objects.none()