10👍
There are better options, I think:
from django.db.models import Q
Something.objects.filter(~Q(file__isnull=True))
or
Something.objects.exclude(file__isnull=True)
👤simP
- [Django]-Oauth for Google API example using Python / Django
- [Django]-Django, get_absolute_url method for file object
- [Django]-How to get the label of a choice in a Django forms ChoiceField?
7👍
This worked perfectly for me:
objects = MyModel.objects.exclude(
Q(file='')|Q(file=None)
)
https://books.agiliq.com/projects/django-orm-cookbook/en/latest/filefield.html
- [Django]-How do you run a worker with AWS Elastic Beanstalk?
- [Django]-What is the right way to validate if an object exists in a django view without returning 404?
- [Django]-How can I set the field unique in django?
1👍
I think we can directly filter out null values using:
Something.objects.filter(file__isnull=False)
In filter, you can give multiple values but in the exclude clause, you can’t give multiple values. you must have to use chaining with exclude.
for more details of Django filter and exclude you can refer this link
- [Django]-OSQA vs. Askbot?
- [Django]-What is your favorite solution for managing database migrations in django?
- [Django]-How to show a message to a django admin after saving a model?
0👍
Your field allows nulls, so I guess the ones without files have the field as null, not the empty string. Try this:
Something.objects.exclude(file=None)
- [Django]-Django how to pass custom variables to context to use in custom admin template?
- [Django]-Saving Many To Many data via a modelform in Django
- [Django]-Django: what is the difference (rel & field)
Source:stackexchange.com