def sort_by_attribute(self, qs, name):
fields   = self.get_queryset().model._meta.get_all_field_names()<EOL>schemata = self.sortable_names<EOL>direction = '<STR_LIT:->' if self.data.get('<STR_LIT>') else '<STR_LIT>'<EOL>if name in fields:<EOL><INDENT>return qs.order_by('<STR_LIT>' % (direction, name))<EOL><DEDENT>elif name in schemata:<EOL><INDENT>schema = self.get_schema(name)<EOL>value_lookup = '<STR_LIT>' % schema.datatype<EOL>order_lookup = '<STR_LIT>' % (direction, value_lookup)<EOL>return qs.filter(attrs__schema__name=name).order_by(order_lookup)<EOL><DEDENT>else:<EOL><INDENT>raise NameError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % (name,<EOL>'<STR_LIT:U+002CU+0020>'.join(fields), '<STR_LIT:U+002CU+0020>'.join(schemata)))<EOL><DEDENT>
A wrapper around standard order_by() method. Allows to sort by both normal
fields and EAV attributes without thinking about implementation details.
Usage::

    qs = sort_by_attributes(qs, 'price', 'colour')

...where `price` is a FloatField, and `colour` is the name of an EAV attribute
represented by Schema and Attr models.