| 1 | from django.db.models.query import Q
|
|---|
| 2 |
|
|---|
| 3 | class hacked_Q_for_isnull(Q):
|
|---|
| 4 | """
|
|---|
| 5 | Q object that forces all joins to be LEFT JOINs. This is necessary
|
|---|
| 6 | in the case of a filter for foo_isnull=True.
|
|---|
| 7 |
|
|---|
| 8 | For a bit more detail, see ticket #1050
|
|---|
| 9 | http://code.djangoproject.com/ticket/1050
|
|---|
| 10 |
|
|---|
| 11 | Example usage:
|
|---|
| 12 | Foo.objects.filter(hacked_Q_for_isnull(bar__isnull=True))
|
|---|
| 13 |
|
|---|
| 14 | Note:
|
|---|
| 15 | The above usage is the only one that's been tested. Even so,
|
|---|
| 16 | this is an ugly approach but I hope it will be enough until the
|
|---|
| 17 | QuerySet refactor is completed.
|
|---|
| 18 | """
|
|---|
| 19 | def get_sql(self, opts):
|
|---|
| 20 | results = super(hacked_Q_for_isnull, self).get_sql(opts)
|
|---|
| 21 | new_results = []
|
|---|
| 22 | for d in results:
|
|---|
| 23 | if isinstance(d, dict):
|
|---|
| 24 | temp = {}
|
|---|
| 25 | for k,v in d.items():
|
|---|
| 26 | temp_list = list(v)
|
|---|
| 27 | temp_list[1] = 'LEFT JOIN'
|
|---|
| 28 | temp[k] = temp_list
|
|---|
| 29 | d=temp
|
|---|
| 30 | new_results.append(d)
|
|---|
| 31 | return new_results
|
|---|