Closes #6138: Add an 'empty' filter modifier for character fields

This commit is contained in:
jeremystretch
2021-07-01 15:17:46 -04:00
parent 0a77051c9d
commit b03a0a9a8f
6 changed files with 45 additions and 23 deletions

17
netbox/extras/lookups.py Normal file
View File

@@ -0,0 +1,17 @@
from django.db.models import CharField, Lookup
class Empty(Lookup):
"""
Filter on whether a string is empty.
"""
lookup_name = 'empty'
def as_sql(self, qn, connection):
lhs, lhs_params = self.process_lhs(qn, connection)
rhs, rhs_params = self.process_rhs(qn, connection)
params = lhs_params + rhs_params
return 'CAST(LENGTH(%s) AS BOOLEAN) != %s' % (lhs, rhs), params
CharField.register_lookup(Empty)