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
distinct() Returns a new QuerySet that uses SELECT DISTINCT in its SQL query. This eliminates duplicate rows from the query results. By default, a QuerySet will not eliminate duplicate rows.
We can use a distinct() query method at the end or in between queryset and with other regular Django query methods like all(), values(), etc…
select_related() “follows” foreign-key relationships, selecting additional related-object data when it executes its query. prefetch_related() does a separate lookup for each relationship and does the “joining” in Python.
Django offers a QuerySet method called select_related() that allows you to retrieve related objects for one-to-many relationships. This translates to a single, more complex QuerySet, but you avoid additional queries when accessing the related objects. The select_related method is for ForeignKey and OneToOne fields.
Try this:
from django.db.models import Q
example.objects.exclude(
Q(values__isnull=True)|Q(values='')
).values_list('values', flat=True).distinct()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With