4

I have an QuerySet with one 'extra' field:

query_set = data_model.extra(select={'status': '''CASE  <sql-query here>''')

But when I try to exclude or filter this field:

query_set = query_set.exclude(status=1)
query_set = query_set.filter(status=2)

I get an error:

Cannot resolve keyword 'status' into field. Choices are: <list of field from my models 
here>

I understand that 'extra' means using pure SQL (without ORM), but 'exclude' or 'filter' method are 'ORM' methods. But in this case why other methods, for example 'values_list', works fine:

values = query_set.values_list('status')

The code above does not generate any exceptions. Thus how can I filter or exclude fields which was got with 'extra' method?

This is a quite simple model (but with a lots of fields), but this is the 'extra' method which I use:

qs = qs.extra(select={
    'void_status': '''CASE
        WHEN "orderitem".voided_date IS NULL
            THEN {normal}
        WHEN "orderitem".voided_date >= %s AND
             "orderitem".voided_date < %s AND
             "orderitem".created_date >= %s AND
             "orderitem".created_date < %s
            THEN {fully_voided}
        WHEN "orderitem".voided_date >= %s AND
             "orderitem".voided_date < %s
            THEN {voided}
        ELSE {normal}
    END'''.format(normal=1,
                  voided=2,
                  fully_voided=3)
}, select_params=[start, end] * 3)
Andrey
  • 101
  • 1
  • 8
  • Can you post your models and the extra sql please? – Henrik Andersson Dec 02 '13 at 08:18
  • 2
    possible duplicate of [Django ORM: Filter by extra attribute](http://stackoverflow.com/questions/4348124/django-orm-filter-by-extra-attribute) – tuxcanfly Dec 02 '13 at 08:20
  • Did you mean that I need to use something like qs.extra(where=[...], params=[...])? I have tried this and get DB errors (field is not exist, but this is another issue looks like). Thank you! – Andrey Dec 02 '13 at 08:52

0 Answers0