Support passing a queryset to the search model
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -295,6 +295,7 @@
|
||||
grant and remove action.
|
||||
* Add Latvian translation.
|
||||
* Support search model selection.
|
||||
* Support passing a queryset factory to the search model.
|
||||
|
||||
3.1.11 (2019-04-XX)
|
||||
===================
|
||||
|
||||
@@ -724,6 +724,7 @@ Other changes
|
||||
grant and remove action.
|
||||
- Add Latvian translation.
|
||||
- Support search model selection.
|
||||
- Support passing a queryset factory to the search model.
|
||||
|
||||
|
||||
Removals
|
||||
|
||||
@@ -105,7 +105,7 @@ class SearchModel(object):
|
||||
|
||||
def __init__(
|
||||
self, app_label, model_name, serializer_path, label=None,
|
||||
list_mode=None, permission=None
|
||||
list_mode=None, permission=None, queryset=None
|
||||
):
|
||||
self.app_label = app_label
|
||||
self.list_mode = list_mode or LIST_MODE_CHOICE_LIST
|
||||
@@ -115,6 +115,7 @@ class SearchModel(object):
|
||||
self._label = label
|
||||
self.serializer_path = serializer_path
|
||||
self.permission = permission
|
||||
self.queryset = queryset
|
||||
self.__class__._registry[self.get_full_name()] = self
|
||||
|
||||
def __str__(self):
|
||||
@@ -140,6 +141,12 @@ class SearchModel(object):
|
||||
def get_full_name(self):
|
||||
return '%s.%s' % (self.app_label, self.model_name)
|
||||
|
||||
def get_queryset(self):
|
||||
if self.queryset:
|
||||
return self.queryset()
|
||||
else:
|
||||
return self.model.objects.all()
|
||||
|
||||
def get_search_field(self, full_name):
|
||||
try:
|
||||
return self.search_fields[full_name]
|
||||
@@ -177,7 +184,7 @@ class SearchModel(object):
|
||||
query_string=query_string, global_and_search=global_and_search
|
||||
)
|
||||
|
||||
queryset = self.model.objects.filter(search_query.query).distinct()
|
||||
queryset = self.get_queryset().filter(search_query.query).distinct()
|
||||
|
||||
if self.permission:
|
||||
queryset = AccessControlList.objects.restrict_queryset(
|
||||
|
||||
Reference in New Issue
Block a user