1 | #encoding= utf-8
|
---|
2 | from django.db import models
|
---|
3 | from django.contrib.auth.models import User
|
---|
4 |
|
---|
5 | Order(models.Model):
|
---|
6 | created_by = models.ForeignKey(User)
|
---|
7 | text = models.TextField()
|
---|
8 |
|
---|
9 | searchstring = 'Lara' #some user that exists and has orders
|
---|
10 |
|
---|
11 | orders = Order.objects.all()
|
---|
12 | orders = orders.extra(where=[u"((first_name||' '||last_name) ILIKE '%%'|| %s ||'%%' or username = %s)",
|
---|
13 | u"(auth_user.id = core_order.created_by_id OR auth_user.id = core_order.last_modified_by_id)"],
|
---|
14 | params=[searchstring, searchstring],
|
---|
15 | tables=['auth_user'])
|
---|
16 |
|
---|
17 | print orders.query.as_sql()
|
---|
18 | orders = orders.order_by('-created_by')
|
---|
19 |
|
---|
20 | print orders #This works fine
|
---|
21 |
|
---|
22 | orders = Order.objects.all()
|
---|
23 | orders = orders.extra(where=[u"((first_name||' '||last_name) ILIKE '%%'|| %s ||'%%' or username = %s)",
|
---|
24 | u"(auth_user.id = core_order.created_by_id OR auth_user.id = core_order.last_modified_by_id)"],
|
---|
25 | params=[searchstring, searchstring],
|
---|
26 | tables=['auth_user'])
|
---|
27 | #Omitting as_sql() this time
|
---|
28 | orders = orders.order_by('-created_by')
|
---|
29 |
|
---|
30 | print orders
|
---|
31 | Traceback (most recent call last):
|
---|
32 | File "<console>", line 1, in <module>
|
---|
33 | ...
|
---|
34 | ProgrammingError: column "first_name" does not exist
|
---|
35 | LINE 1: ..."."last_modified_by_id" FROM "core_order" WHERE ((first_name...
|
---|
36 |
|
---|