Use autocomplete for list of wishlists (WIP)

Note to self: forgetting to add dal to INSTALLED_APPS leads to names in
it not being available, although dal itself exists.
newer-dal
LEdoian 4 years ago
parent 2b12c8fc9e
commit a50fb8ce24

@ -38,6 +38,9 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'dal',
'dal_select2',
'utils', 'utils',
'wishlist', 'wishlist',
] ]

@ -4,10 +4,16 @@ import wishlist.models as m
admin.site.register(m.WishList) admin.site.register(m.WishList)
from django.forms import ModelForm from django.forms import ModelForm
from dal import autocomplete
class WishedItemAdminForm(ModelForm): class WishedItemAdminForm(ModelForm):
class Meta: class Meta:
model = m.WishedItem model = m.WishedItem
fields = '__all__' fields = '__all__'
widgets = {
'wishlists': autocomplete.ModelSelect2Multiple(
url='wishlist_list_autocomplete',
),
}
@admin.register(m.WishedItem) @admin.register(m.WishedItem)
class WishedItemAdmin(admin.ModelAdmin): class WishedItemAdmin(admin.ModelAdmin):

@ -1,8 +1,10 @@
from django.urls import path from django.urls import path
from . import views as v from . import views as v
from . import views_autocomplete as ac
urlpatterns = [ urlpatterns = [
path('user/<str:loginname>', v.UserWishLists.as_view(), name='wishlists_user'), path('user/<str:loginname>', v.UserWishLists.as_view(), name='wishlists_user'),
path('item/<int:pk>', v.WishedItemView.as_view(), name='wished_item'), path('item/<int:pk>', v.WishedItemView.as_view(), name='wished_item'),
path('<slug:slug>', v.WishListView.as_view(), name='wishlist'), path('<slug:slug>', v.WishListView.as_view(), name='wishlist'),
path('autocomplete/wishlists', ac.WishListAutocomplete.as_view(), name='wishlist_list_autocomplete'),
] ]

@ -0,0 +1,14 @@
from dal import autocomplete
from wishlist.models import WishList
class WishListAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated:
return WishList.objects.none()
qs = WishList.objects.all().filter(owner=self.request.user)
if self.q:
qs = qs.filter(name__startswith=self.q)
return qs
Loading…
Cancel
Save