1

Given the model:

class Person(models.Model):
    name = models.CharField(max_length=200)
    resume = models.FileField(upload_to='resumes/', blank=True, null=True)

I have created a few people via:

Person.objects.create(name='Brad')
Person.objects.create(name='Tony')

How do I write a query using the ORM to give me people that don't have a resume. I've tried the following, but all fail to return the desired result:

>>> Person.objects.filter(resume=None)
[]
>>> Person.objects.filter(resume__exact=None)
[]
>>> Person.objects.filter(resume__isnull=True)
[]
bradley.ayers
  • 37,165
  • 14
  • 93
  • 99
  • 1
    Dup: http://stackoverflow.com/questions/844556/django-filter-how-do-i-go-about-filtering-for-emply-or-null-names-in-a-queryse – XORcist Apr 15 '11 at 05:14
  • 1
    This isn't a duplicate, they're different questions. – bradley.ayers Apr 15 '11 at 05:19
  • 1
    This seems to be a different question, because it deals with FileFields which require a different solution because they are CharFields. It's good information. The other question doesn't discuss this situation. – dting Apr 15 '11 at 05:35
  • Please define "fail". Since we don't know what "fail" means, we can't really guess what's wrong with the sample questies. Also ``file`` fields are special, since they're not actually **in** the database. – S.Lott Apr 15 '11 at 10:17
  • @S.Lott I've edited the question to clarify. – bradley.ayers Apr 15 '11 at 10:20

2 Answers2

1

I'm pretty sure that you filter by empty string ''

Person.objects.filter(resume='')

The underlying field is a CharField() as mentioned by the docs for Filefield.

Django stores blanks for CharFields as empty strings not NULL, so filtering for empty strings gets you Persons with no resumes.

Model Field References regarding Null

dting
  • 38,604
  • 10
  • 95
  • 114
  • Thanks, that works for me. Will a `FileField` *ever* be `None`? – bradley.ayers Apr 15 '11 at 05:18
  • Check out the blog post [`here`](http://www.b-list.org/weblog/2006/jun/28/django-tips-difference-between-blank-and-null/) – dting Apr 15 '11 at 05:19
  • The docs for `FileField` don't mention anything about having to check for an empty string rather than `None`, and neither does that blog post. – bradley.ayers Apr 15 '11 at 05:23
  • 1
    @bradly.ayers the docs talk about the underlying field as a CharField, and the blog talks about why null is stored as an empty string in the db for CharFields. – dting Apr 15 '11 at 05:24
  • @kriegar Which paragraph in the blog post talks about that? – bradley.ayers Apr 15 '11 at 05:27
  • @bradly.ayers i should have just linked [`this`](http://docs.djangoproject.com/en/1.3/ref/models/fields/#null) but the comments from that blog post discuss this issue. I guess the blog is another level removed from the underlying fact that blank CharFields are stored as empty strings. Sorry. – dting Apr 15 '11 at 05:32
  • Please edit your answer to link to that null documentation as the rationale for the solution. – bradley.ayers Apr 15 '11 at 05:40
0

try

Person.objects.filter(resume__exact = None)

read the document here

http://docs.djangoproject.com/en/dev/ref/models/querysets/#id6

ftao
  • 441
  • 3
  • 5