Skip to content

Commit

Permalink
feat(backend): add not equal (__ne) and not in (__notin) custom lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
pablolmedorado committed Jun 19, 2022
1 parent 6d89c5d commit 2bf9cb7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backend/common/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
class CommonConfig(AppConfig):
name = "common"
verbose_name = _("común")

def ready(self):
import common.lookups # noqa
21 changes: 21 additions & 0 deletions backend/common/lookups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.db.models import Field, Lookup
from django.db.models.lookups import In


@Field.register_lookup
class NotEqual(Lookup):
lookup_name = "ne"

def as_sql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(compiler, connection)
params = lhs_params + rhs_params
return "%s <> %s" % (lhs, rhs), params


@Field.register_lookup
class NotIn(In):
lookup_name = "notin"

def get_rhs_op(self, connection, rhs):
return "NOT IN %s" % rhs
6 changes: 5 additions & 1 deletion backend/work_organization/api/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ class Meta:
class SupportWorkingDayFilterSet(django_filters.rest_framework.FilterSet):
class Meta:
model = SupportWorkingDay
fields = {"id": ["exact", "in"], "user_id": ["exact", "in"], "date": ["exact", "gte", "lte", "year"]}
fields = {
"id": ["exact", "in"],
"user_id": ["exact", "in"],
"date": ["exact", "gte", "lte", "year"],
}


class HolidayFilterSet(django_filters.rest_framework.FilterSet):
Expand Down

0 comments on commit 2bf9cb7

Please sign in to comment.