The custom model fields documentation for get_db_prep_lookup() show the ability to raise a TypeError as a way to prevent certain types of lookups from taking place. In practice, however, it seems this error gets suppressed somewhere along the line, result in the QuerySet? returning an empty list.
The query doesn't actually get executed in the database, as it's possible to observe this behavior even without having a true database table backing up the model. Also, while TypeError gets eaten, ValueError passes through just fine. I haven't tried all error types, but AttributeError also gets suppressed in the same manner as TypeError.
Also of interest, when manually iterating over a QuerySet? that should raise a TypeError, it does in fact raise the error appropriately. However, when simply calling list() on it, the bug shows up. The following interpreter session illustrates the problem and its many peculiarities.
>>> from django.db import models
>>> class TypeErrorField(models.TextField):
... def get_db_prep_lookup(self, lookup_type, value):
... raise TypeError("Lookups are prohibited.")
>>> class ValueErrorField(models.TextField):
... def get_db_prep_lookup(self, lookup_type, value):
... raise ValueError("Lookups are prohibited.")
>>> class Test(models.Model):
... type_error = TypeErrorField()
... value_error = ValueErrorField()
... class Meta:
... app_label = 'test'
>>> Test.objects.filter(type_error='test')
[]
>>> list(Test.objects.filter(type_error='test'))
[]
>>> [x for x in Test.objects.filter(type_error='test')]
Traceback (most recent call last):
...
TypeError: Lookups are prohibited.
>>> Test.objects.filter(value_error='test')
Traceback (most recent call last):
...
ValueError: Lookups are prohibited.
Whether the documentation is wrong, or the documentation, I'm not sure, but there's certainly a disconnect here. If it matters, this was tested on Windows Vista, Python 2.5, SQLite. I've tried to trace through the execution, but I was unable to find anything that would be suppressing the error.