1

How to get all field from DB where value have number?

Example valid: 1test, tes1t, test1

I suppose something like this: field__contains

Alasdair
  • 298,606
  • 55
  • 578
  • 516
milandjukic88
  • 1,065
  • 3
  • 13
  • 30

2 Answers2

8

You can do this with a regex query.

Model.objects.filter(value__regex=r'\d')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

Use icontains: django docs.

objs = Model.objects.filter(value__icontains="1")

The equivalent of doing an sql ILIKE "%1%" (case insensitive)

contains may work just as well actually if it's a number, that's a simple LIKE

Edit: To answer your comment yes you'd need to loop over the numbers. See this question

Community
  • 1
  • 1
ptr
  • 3,292
  • 2
  • 24
  • 48