yourlabs / django-autocomplete-light

A fresh approach to autocomplete implementations, specially for Django. Status: v4 alpha, v3 stable, v2 & v1 deprecated.
https://django-autocomplete-light.readthedocs.io
MIT License
1.79k stars 467 forks source link

"No results found" - but when I go in admin, they are found - and then starts popping up at the web #1277

Closed minifisk closed 2 years ago

minifisk commented 2 years ago

If I restart my container and go to the website and go to DAL field, I'm met with "no results found", however if I go into my admin site and try creating a model which is connected to DAL, I get the results. And as soon as I get them I can also start seeing them in the browser again. If I then go to my page in incognito they are no longer there - so I guess they are cached in some way but can only get cached by going into the admin panel. Any ideas on how to resolve this?

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)
minifisk commented 2 years ago

Had to do with " if not self.request.user.is_authenticated:"