6

I have a table as follows:

table_name : example

values
1
1
2

2
3

3

the value list contains the values as above. When i write a distinct query on the able table, i get 1 2 3 and a empty '' value.

I want to get rid of the null value as it is posing some problems in my program. the query for that which i have written is:

example.objects.values_list('values',flat = True).distinct()

Is there a a way to filter out that empty value

1
  • Sorry, voted down by mistake, can't unvote until the question is edited...
    – o_c
    Feb 16, 2016 at 11:17

4 Answers 4

8

Try this:

from django.db.models import Q
example.objects.exclude(
      Q(values__isnull=True)|Q(values='')
    ).values_list('values', flat=True).distinct()
3

As other answers mentioned, if you have a null/empty string confusion. You might exclude both using complex lookup parameters like:

from django.db.models import Q
example.objects.exclude(Q(values__isnull=True)|Q(values='')).values_list('values',flat = True).distinct()

Complex Lookups are here...

4
  • You have to use the | between the Q's not within. Sep 4, 2012 at 12:53
  • @FallenAngel: The code works now. But if i get a syntax error if i use both the values_isnull and values. But when i used only the values = '' the null values were not displayed.Thank you for your help
    – sankar
    Sep 4, 2012 at 12:54
  • @sankar i fixed my typo, you can use it now
    – Mp0int
    Sep 4, 2012 at 13:15
  • 1
    Sorry, i make a mistake in Q usage, it is fixed now, Thank you @AndreBossard
    – Mp0int
    Sep 4, 2012 at 13:15
1

You can try filtering out the null values first and then chain your filter, as below

example.objects.exclude(values=None).values_list('values',flat = True).distinct()
2
  • If you only pass in a single field, you can also pass in the flat parameter. If True, this will mean the returned results are single values, rather than one-tuples. An example should make the difference clearer (docs.djangoproject.com/en/1.4/ref/models/querysets/#values-list)
    – Mp0int
    Sep 4, 2012 at 12:32
  • i tried this code, but still i am getting null values in my list
    – sankar
    Sep 4, 2012 at 12:47
0

Try to use is_null field lookup:

example.objects.filter(values__isnull=False).values_list('values',flat = True).distinct()

So query string before .values_list(... will return all records from example table, which values field is not null.

As @BurhanKhalid has noticed, this attempt will work only if your values field is NULL-able

3
  • This is a good idea but it would only work if the field is NULL-able; but from the output it seems the field has a default value of "" (an empty string). Sep 4, 2012 at 12:31
  • @BurhanKhalid it's true. My answer based on that assumption Sep 4, 2012 at 12:35
  • i have tried this code, but it does not seem to work. i still get a list with null values
    – sankar
    Sep 4, 2012 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.