From: Stephen Finucane Date: Wed, 11 Apr 2018 16:13:33 +0000 (+0100) Subject: REST: Simplify ModelMultiChoiceField X-Git-Tag: v2.1.0-rc2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc53930e8b55ff151bfc11a6e80a3d352ab7fb35;p=thirdparty%2Fpatchwork.git REST: Simplify ModelMultiChoiceField We're actually going to remove this shortly but the new technique works for both the current approach and the approach we adopt in future patches. Signed-off-by: Stephen Finucane [dja: commit message, drop dead code] Signed-off-by: Daniel Axtens --- diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py index 25956e98..113ab8d3 100644 --- a/patchwork/api/filters.py +++ b/patchwork/api/filters.py @@ -39,14 +39,14 @@ from patchwork.models import State class ModelMultiChoiceField(ModelChoiceField): - def _get_filters(self, value): - raise NotImplementedError - def to_python(self, value): if value in self.empty_values: return None - filters = self._get_filters(value) + try: + filters = {'pk': int(value)} + except ValueError: + filters = {self.alternate_lookup: value} try: value = self.queryset.get(**filters) @@ -58,11 +58,7 @@ class ModelMultiChoiceField(ModelChoiceField): class ProjectChoiceField(ModelMultiChoiceField): - def _get_filters(self, value): - try: - return {'pk': int(value)} - except ValueError: - return {'linkname__iexact': value} + alternate_lookup = 'linkname__iexact' class ProjectFilter(ModelChoiceFilter): @@ -72,11 +68,7 @@ class ProjectFilter(ModelChoiceFilter): class PersonChoiceField(ModelMultiChoiceField): - def _get_filters(self, value): - try: - return {'pk': int(value)} - except ValueError: - return {'email__iexact': value} + alternate_lookup = 'email__iexact' class PersonFilter(ModelChoiceFilter): @@ -111,11 +103,7 @@ class StateFilter(ModelChoiceFilter): class UserChoiceField(ModelMultiChoiceField): - def _get_filters(self, value): - try: - return {'pk': int(value)} - except ValueError: - return {'username__iexact': value} + alternate_lookup = 'username__iexact' class UserFilter(ModelChoiceFilter):