简体   繁体   中英

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

I have a peculiar problem with Django-autocomplete-light. When I go to my browser and try searching for something I get "no results found" - but when I go to the admin panel and add a model, the results pop up as they should. When I have done this one time, I can go back to the browser and then the results show up as they should. I'm suspecting some caching issue, but not sure how to debug this. Maybe someone can take a look if there is something wrong with my set-up.

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

Form template

{% 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)

The issue was resolved by removing the following lines, as per the recommendation by the good Iain Shelvington!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM