Wild card search #3702

Closed
opened 2025-12-29 18:30:41 +01:00 by adam · 1 comment
Owner

Originally created by @ebusto on GitHub (May 18, 2020).

Environment

  • Python version: 3.7.7
  • NetBox version: 2.8.4

Proposed Functionality

Currently searches, that is, the search methods in various FilterSet classes, only perform substring matches. It would be nifty is users could specify wildcards, such as the SQL style %.

I've implemented this functionality for one of our plugins, so users can search on the name and description with %.

from django.db.models import CharField, Lookup, TextField

class InsensitiveLikeLookup(Lookup):
	lookup_name = 'ilike'

	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 ILIKE %s' % (lhs, rhs), params

CharField.register_lookup(InsensitiveLikeLookup)
TextField.register_lookup(InsensitiveLikeLookup)

Regex searches would also be nice, as PostgreSQL natively supports regular expressions.

Use Case

We have a large number of devices and virtual machines in NetBox, so this would help our users issue more concise searches that don't return records they don't care about.

Database Changes

None.

External Dependencies

None.

Originally created by @ebusto on GitHub (May 18, 2020). ### Environment * Python version: 3.7.7 * NetBox version: 2.8.4 ### Proposed Functionality Currently searches, that is, the `search` methods in various FilterSet classes, only perform substring matches. It would be nifty is users could specify wildcards, such as the SQL style `%`. I've implemented this functionality for one of our plugins, so users can search on the name and description with `%`. ``` from django.db.models import CharField, Lookup, TextField class InsensitiveLikeLookup(Lookup): lookup_name = 'ilike' 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 ILIKE %s' % (lhs, rhs), params CharField.register_lookup(InsensitiveLikeLookup) TextField.register_lookup(InsensitiveLikeLookup) ``` Regex searches would also be nice, as PostgreSQL natively supports regular expressions. ### Use Case We have a large number of devices and virtual machines in NetBox, so this would help our users issue more concise searches that don't return records they don't care about. ### Database Changes None. ### External Dependencies None.
adam closed this issue 2025-12-29 18:30:41 +01:00
Author
Owner

@jeremystretch commented on GitHub (May 26, 2020):

Given that this functionality would not be specific to NetBox, it seems better suited as a feature request raised against django-filter itself. I'm hesitant to add yet more custom filters that we have to maintain, but if it gets added upstream I have no problem with implementing it.

@jeremystretch commented on GitHub (May 26, 2020): Given that this functionality would not be specific to NetBox, it seems better suited as a feature request raised against [django-filter](https://github.com/carltongibson/django-filter) itself. I'm hesitant to add yet more custom filters that we have to maintain, but if it gets added upstream I have no problem with implementing it.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3702