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 typing import Generic, Iterable, Type, TypeVar
|
||||||
from django.db.models import Q
|
|
||||||
|
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.
|
"""Basic search engine for models based on filtering.
|
||||||
|
|
||||||
Subclasses should override the ``model`` class attribute and specify the list of
|
Subclasses should override the ``model`` class attribute and specify the list of
|
||||||
search fields to be searched in.
|
search fields to be searched in.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
model = None
|
model: Type[M]
|
||||||
search_fields = []
|
search_fields: Iterable[str]
|
||||||
|
|
||||||
def get_queryset_filter(self, keywords):
|
def get_queryset_filter(self, keywords: Iterable[str]) -> Q:
|
||||||
filter_q = Q()
|
filter_q = Q()
|
||||||
|
|
||||||
if not keywords:
|
if not keywords:
|
||||||
|
@ -26,7 +30,7 @@ class ModelSearch:
|
||||||
|
|
||||||
return filter_q
|
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.
|
"""Returns the queryset of model instances matching all the keywords.
|
||||||
|
|
||||||
The semantic of the search is the following: a model instance appears in the
|
The semantic of the search is the following: a model instance appears in the
|
||||||
|
|
Loading…
Add table
Reference in a new issue