Django: Filtering a model containing a field in which Regex is stored

I have a field in which REGEX templates are stored, and I'm trying to filter out the model in which it is located, comparing it with the hostname passed in the variable. (Example: here I just encoded REGEX.

Sys_team.objects.filter(hostname= r'^.*\.amgr\..*')

I met this error:

FieldError: Cannot resolve keyword 'hostname' into field. Choices are: alert, id, pattern, pub_date, sys_team

The host name has the format: xxx.amgr.xxx

Does this mean that only fields can go on the left side of the filter? And if so, is there any other way to compare these two with the REGEX pattern on the left side. To repeat, the host name is not a field.

+4
source share
1 answer

Use the Django method __contains.

So for your request:

Sys_team.objects.filter(hostname__contains='.amgr.')

__contains Django ORM SQL LIKE.

docs:

https://docs.djangoproject.com/en/dev/topics/db/queries/#escaping-percent-signs-and-underscores-in-like-statements

+5

All Articles