Drop type hints in shared.views.autocomplete
This commit is contained in:
parent
a259b04d9c
commit
b8cd5f1da5
1 changed files with 10 additions and 19 deletions
|
@ -1,20 +1,13 @@
|
||||||
from typing import Generic, Iterable, Type, TypeVar
|
from dal import autocomplete
|
||||||
|
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(Generic[M]):
|
class ModelSearch:
|
||||||
"""Basic search engine for models based on filtering.
|
"""Basic search engine for models based on filtering.
|
||||||
|
|
||||||
As the type hints indicate, the class is generic with respect to the model. This
|
The class should be configured through its ``model`` class attribute: the ``search``
|
||||||
means that the ``search`` method only returns instances of the model specified as
|
method will return a queryset of instances of this model. The ``search_fields``
|
||||||
the ``model`` class attribute in subclasses.
|
attributes indicates which fields to search in.
|
||||||
|
|
||||||
The ``search_fields`` attributes indicates which fields to search in during the
|
|
||||||
search.
|
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
@ -28,12 +21,10 @@ class ModelSearch(Generic[M]):
|
||||||
>>> user_search.search(["toto", "foo"]) # returns a queryset of Users
|
>>> user_search.search(["toto", "foo"]) # returns a queryset of Users
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# This says that `model` is the class corresponding to the type variable M (or a
|
model = None
|
||||||
# subclass).
|
search_fields = []
|
||||||
model: Type[M]
|
|
||||||
search_fields: Iterable[str]
|
|
||||||
|
|
||||||
def get_queryset_filter(self, keywords: Iterable[str]) -> Q:
|
def get_queryset_filter(self, keywords):
|
||||||
filter_q = Q()
|
filter_q = Q()
|
||||||
|
|
||||||
if not keywords:
|
if not keywords:
|
||||||
|
@ -47,7 +38,7 @@ class ModelSearch(Generic[M]):
|
||||||
|
|
||||||
return filter_q
|
return filter_q
|
||||||
|
|
||||||
def search(self, keywords: Iterable[str]) -> Iterable[M]:
|
def search(self, keywords):
|
||||||
"""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…
Reference in a new issue