forked from DGNum/gestioCOF
Type hints in shared.views.autocomplete
This commit is contained in:
parent
914888d18a
commit
d2c6c9da7a
1 changed files with 11 additions and 7 deletions
|
@ -1,18 +1,22 @@
|
|||
from dal import autocomplete
|
||||
from django.db.models import Q
|
||||
from typing import Generic, Iterable, Type, TypeVar
|
||||
|
||||
from dal import autocomplete # type: ignore
|
||||
from django.db.models import Model, Q
|
||||
|
||||
M = TypeVar("M", bound=Model)
|
||||
|
||||
|
||||
class ModelSearch:
|
||||
class ModelSearch(Generic[M]):
|
||||
"""Basic search engine for models based on filtering.
|
||||
|
||||
Subclasses should override the ``model`` class attribute and specify the list of
|
||||
search fields to be searched in.
|
||||
"""
|
||||
|
||||
model = None
|
||||
search_fields = []
|
||||
model: Type[M]
|
||||
search_fields: Iterable[str]
|
||||
|
||||
def get_queryset_filter(self, keywords):
|
||||
def get_queryset_filter(self, keywords: Iterable[str]) -> Q:
|
||||
filter_q = Q()
|
||||
|
||||
if not keywords:
|
||||
|
@ -26,7 +30,7 @@ class ModelSearch:
|
|||
|
||||
return filter_q
|
||||
|
||||
def search(self, keywords):
|
||||
def search(self, keywords: Iterable[str]) -> Iterable[M]:
|
||||
"""Returns the queryset of model instances matching all the keywords.
|
||||
|
||||
The semantic of the search is the following: a model instance appears in the
|
||||
|
|
Loading…
Reference in a new issue