2

I have a model Chair with a blank-able CharField called wood_type.

I want to filter all the chairs with a wood_type which is not ''.

What's an elegant way to do it with Django?

I can think of this:

Chair.objects.filter(~django.db.models.Q(wood_type=''))

Or this:

Chair.objects.filter(wood_type__regex='(.|\n)+')

But they're ugly as hell. Is there a more elegant way?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
  • 1
    See http://stackoverflow.com/questions/844556/django-filter-how-do-i-go-about-filtering-for-empty-or-null-names-in-a-querys – Hakan B. Aug 11 '13 at 19:28

2 Answers2

7

Chair.objects.exclude(wood_type='') should do the trick.

Maciej Gol
  • 15,394
  • 4
  • 33
  • 51
0

exclude the empty string

Chair.objects.exclude(wood_type='')
drabo2005
  • 1,076
  • 1
  • 10
  • 16