1
1
from IPy import IP
2
2
3
+ from types import StringTypes
3
4
from django .db import models , connection
4
5
from django .db .models import sql , query
5
6
from django .db .models .query_utils import QueryWrapper
@@ -33,19 +34,26 @@ def add_filter(self, (filter_string, value), *args, **kwargs):
33
34
34
35
35
36
class NetWhere (sql .where .WhereNode ):
36
- def make_atom (self , child , qn ):
37
- table_alias , name , db_type , lookup_type , value_annot , params = child
37
+ def make_atom (self , child , qn , conn ):
38
+ if isinstance (child [0 ] , sql .where .Constraint ):
39
+ c = child [0 ]
40
+ table_alias = c .alias
41
+ name = c .col
42
+ field = c .field
43
+ lookup_type , value_annot , params = child [1 :]
44
+ else :
45
+ table_alias , name , db_type , lookup_type , value_annot , params = child
38
46
39
- if db_type not in ['inet' , 'cidr' ]:
40
- return super (NetWhere , self ).make_atom (child , qn )
47
+ if field . db_type () not in ['inet' , 'cidr' ]:
48
+ return super (NetWhere , self ).make_atom (child , qn , conn )
41
49
42
50
if table_alias :
43
51
field_sql = '%s.%s' % (qn (table_alias ), qn (name ))
44
52
else :
45
53
field_sql = qn (name )
46
54
47
55
if NET_OPERATORS .get (lookup_type , '' ) in NET_TEXT_OPERATORS :
48
- if db_type == 'inet' :
56
+ if field . db_type () == 'inet' :
49
57
field_sql = 'HOST(%s)' % field_sql
50
58
else :
51
59
field_sql = 'TEXT(%s)' % field_sql
@@ -55,6 +63,9 @@ def make_atom(self, child, qn):
55
63
else :
56
64
extra = ''
57
65
66
+ if type (params ) in StringTypes :
67
+ params = (params ,)
68
+
58
69
if lookup_type in NET_OPERATORS :
59
70
return (' ' .join ([field_sql , NET_OPERATORS [lookup_type ], extra ]), params )
60
71
elif lookup_type == 'in' :
@@ -71,9 +82,9 @@ def make_atom(self, child, qn):
71
82
raise ValueError ('Invalid lookup type "%s"' % lookup_type )
72
83
73
84
74
- class NetManger (models .Manager ):
85
+ class NetManager (models .Manager ):
75
86
use_for_related_fields = True
76
87
77
88
def get_query_set (self ):
78
- q = NetQuery (self .model , connection , NetWhere )
89
+ q = NetQuery (self .model , NetWhere )
79
90
return query .QuerySet (self .model , q )
0 commit comments