Django ORM query for empty FileFields
<p>It's not clear to me whether this is possible through the ORM - it doesn't appear so, but maybe I'm missing something?</p>

<pre><code>   class ApplicationInstitution(models.Model):
       ...
       transcript_file = FileField(upload_to=...)
</code></pre>

<p>Some of those <code>ApplicationInstitution</code> objects may already have had a <code>transcript_file</code> uploaded into them, while others have not. I want to query for instances with an empty FileField. At first I thought I could so something like</p>

<p><code>ApplicationInstitution.objects.filter(transcript_file__isnull=False)</code></p>

<p>or similar, but this gives unexpected results (either all records or zero, depending on whether you use <code>filter</code> or <code>exclude</code>). And if you try to access the actual file object, you get a Raise, not an empty:</p>

<pre><code>&gt;&gt;&gt; a.institutions.last().transcript_file.file
Traceback (most recent call last):
...
ValueError: The 'transcript_file' attribute has no file associated with it.
</code></pre>

<p>I've written a model method using try/except that gets me the answer, but ORM would be cleaner. Is a query for empty or non-empty FileFields even possible? </p>

<pre><code>transcript_file = FileField(upload_to=..., null=True)
</code></pre>

<p>and try this for <code>ApplicationInstitution</code> without file:</p>

<pre><code>ApplicationInstitution.objects.filter(transcript_file__isnull=True)
</code></pre>

<p>for <code>ApplicationInstitution</code> with file:</p>

<pre><code>ApplicationInstitution.objects.exclude(transcript_file__isnull=True)
</code></pre>

python django orm