diff --git a/djangocms_link/fields_select2.py b/djangocms_link/fields_select2.py index 29a3e55d..7b98a661 100644 --- a/djangocms_link/fields_select2.py +++ b/djangocms_link/fields_select2.py @@ -4,6 +4,8 @@ from django_select2.forms import ModelSelect2Widget +from djangocms_link.helpers import get_queryset_manager + class Select2PageSearchFieldMixin: search_fields = [ @@ -28,15 +30,10 @@ def build_attrs(self, base_attrs, extra_attrs=None): def get_queryset(self): # django CMS < 4 - if hasattr(Page.objects, "drafts"): - if self.site: - return Page.objects.drafts().on_site(self.site) - return Page.objects.drafts() - - # django CMS >= 4 + base_queryset = get_queryset_manager(Page.objects) if self.site: - return Page.objects.on_site(self.site) - return Page.objects.all() + return base_queryset.on_site(self.site) + return base_queryset.all() # we need to implement jQuery ourselves, see #180 class Media: diff --git a/djangocms_link/forms.py b/djangocms_link/forms.py index 43961bb6..6700ff09 100644 --- a/djangocms_link/forms.py +++ b/djangocms_link/forms.py @@ -3,6 +3,8 @@ from djangocms_attributes_field.widgets import AttributesWidget +from djangocms_link.helpers import get_queryset_manager + from .fields import PageSearchField from .models import Link @@ -18,7 +20,7 @@ def for_site(self, site): # current site # this will work for PageSelectFormField from cms.models import Page - self.fields['internal_link'].queryset = Page.objects.on_site(site) + self.fields['internal_link'].queryset = get_queryset_manager(Page.objects).on_site(site) # set the current site as a internal_link field instance attribute # this will be used by the field later to properly set up the queryset # this will work for PageSearchField diff --git a/djangocms_link/helpers.py b/djangocms_link/helpers.py new file mode 100644 index 00000000..e507114c --- /dev/null +++ b/djangocms_link/helpers.py @@ -0,0 +1,4 @@ +def get_queryset_manager(base): + if hasattr(base, "drafts"): + return base.drafts() + return base