0
| random_code         | varchar(200)  | YES  | UNI | NULL    |                |

MyTable.objects.filter(random_code = None)

Is this correct? Will this SELECT where there is no random code set? Above is my table.

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • possible duplicate of [Filtering for empty or NULL names in a queryset](http://stackoverflow.com/questions/844556/filtering-for-empty-or-null-names-in-a-queryset) – Ismael Apr 20 '14 at 08:06

3 Answers3

1

If you ever want to see what your QuerySet as SQL you can use QuerySet.query.as_sql() to return the SQL statement as a string.

Zach
  • 18,594
  • 18
  • 59
  • 68
1

This has been asked here. Basically you do this:

MyTable.objects.filter(random_code__isnull=True)
Community
  • 1
  • 1
Jason Webb
  • 7,938
  • 9
  • 40
  • 49
1

In addition of Jason Webb's answer you could do something like:

MyTable.objects.filter(random_code__isnull=True).exclude(random_code='')

to exclude variants which have empty string.