6

I want to filter mytable & get rows where name does not contain '' or '-' values and for this purpose I have used below query but does not working.

mytable.objects.exclude(name = ['','-']).order_by('name').count() returning 2000 all rows and whereas query mytable.objects.exclude(name = '').order_by('name').count() working perfectly and returning filtered 1460 results.

Below one is my Postgresql query which is working perfectly fine and returning 1450 results excluding name values of ['','-'].

select * from mytable where name != '-' and name != '' order by -id

1 Answer 1

3

Try this.

mytable.objects.exclude(name__in=['', '-'])

This should exclude the rows matching the values you've passed in the list.

And you don't need to do order_by() for getting the count.

Just use the .count() directly on queryset.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks buddy , able to achieve my task & by the way I have used order_by() to sort values by name & count is just for testing purpose.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.