2929
3030
3131def search_relationship (session , instance , relation , filters = None , sort = None ,
32- group_by = None ):
32+ group_by = None , ignorecase = False ):
3333 """Returns a filtered, sorted, and grouped SQLAlchemy query
3434 restricted to those objects related to a given instance.
3535
@@ -40,8 +40,8 @@ def search_relationship(session, instance, relation, filters=None, sort=None,
4040
4141` `relation` is a string naming a to-many relationship of `instance`.
4242
43- `filters`, `sort`, and `group_by ` are identical to the corresponding
44- arguments of :func:`.search`.
43+ `filters`, `sort`, `group_by`, and `ignorecase ` are identical to the
44+ corresponding arguments of :func:`.search`.
4545
4646 """
4747 model = get_model (instance )
@@ -60,11 +60,12 @@ def search_relationship(session, instance, relation, filters=None, sort=None,
6060 query = query .filter (primary_key_value (related_model ).in_ (primary_keys ))
6161
6262 return search (session , related_model , filters = filters , sort = sort ,
63- group_by = group_by , _initial_query = query )
63+ group_by = group_by , ignorecase = ignorecase ,
64+ _initial_query = query )
6465
6566
6667def search (session , model , filters = None , sort = None , group_by = None ,
67- _initial_query = None ):
68+ ignorecase = False , _initial_query = None ):
6869 """Returns a filtered, sorted, and grouped SQLAlchemy query.
6970
7071 `session` is the SQLAlchemy session in which to create the query.
@@ -80,7 +81,9 @@ def search(session, model, filters=None, sort=None, group_by=None,
8081 `sort` is a list of pairs of the form ``(direction, fieldname)``,
8182 where ``direction`` is either '+' or '-' and ``fieldname`` is a
8283 string representing an attribute of the model or a dot-separated
83- relationship path (for example, 'owner.name').
84+ relationship path (for example, 'owner.name'). If `ignorecase` is
85+ True, the sorting will be case-insensitive (so 'a' will precede 'B'
86+ instead of the default behavior in which 'B' precedes 'a').
8487
8588 `group_by` is a list of dot-separated relationship paths on which to
8689 group the query results.
@@ -113,11 +116,15 @@ def search(session, model, filters=None, sort=None, group_by=None,
113116 field_name , field_name_in_relation = field_name .split ('.' )
114117 relation_model = aliased (get_related_model (model , field_name ))
115118 field = getattr (relation_model , field_name_in_relation )
119+ if ignorecase :
120+ field = field .collate ('NOCASE' )
116121 direction = getattr (field , direction_name )
117122 query = query .join (relation_model )
118123 query = query .order_by (direction ())
119124 else :
120125 field = getattr (model , field_name )
126+ if ignorecase :
127+ field = field .collate ('NOCASE' )
121128 direction = getattr (field , direction_name )
122129 query = query .order_by (direction ())
123130 else :
0 commit comments