28 lines
758 B

from dal import autocomplete
from .models import Tag
class AuthenticatedUserCreateMixin():
''' By default, autocomplete-light only allows creation of
choices if the user has add permission on the model. However,
we need to allow any authenticated user to create tags
def has_add_permission(self, request):
print("i am called", request.user)
return request.user.is_authenticated
class TagAutocomplete(
AuthenticatedUserCreateMixin, autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated:
return Tag.objects.none()
qs = Tag.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q)
return qs