Django

Code

Changeset 6511

Show
Ignore:
Timestamp:
10/14/07 19:29:27 (9 months ago)
Author:
mtredinnick
Message:

queryset-refactor: Added an order_by parameter to extra(). Refs #2076.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/queryset-refactor/django/db/models/query.py

    r6501 r6511  
    314314        return obj 
    315315 
    316     def extra(self, select=None, where=None, params=None, tables=None): 
    317         """ 
    318         Add extra SQL fragments to the query. These are applied more or less 
    319         verbatim (no quoting, no alias renaming, etc), so care should be taken 
    320         when using extra() with other complex filters and combinations. 
     316    def extra(self, select=None, where=None, params=None, tables=None, 
     317            order_by=None): 
     318        """ 
     319        Add extra SQL fragments to the query. 
    321320        """ 
    322321        assert self.query.can_filter(), \ 
     
    331330        if tables: 
    332331            clone.query.extra_tables.extend(tables) 
     332        if order_by: 
     333            clone.query.extra_order_by.extend(order_by) 
    333334        return clone 
    334335 
  • django/branches/queryset-refactor/django/db/models/sql/query.py

    r6510 r6511  
    379379            if not self.alias_map[alias][ALIAS_REFCOUNT]: 
    380380                continue 
    381             name, alias, join_type, lhs, lhs_col, col = \ 
    382                     self.alias_map[alias][ALIAS_JOIN] 
    383             alias_str = (alias != name and ' AS %s' % alias or '') 
     381            join = self.alias_map[alias][ALIAS_JOIN] 
     382            if join: 
     383                name, alias, join_type, lhs, lhs_col, col = join 
     384                alias_str = (alias != name and ' AS %s' % alias or '') 
     385            else: 
     386                join_type = None 
     387                alias_str = '' 
     388                name = alias 
    384389            if join_type: 
    385390                result.append('%s %s%s ON (%s.%s = %s.%s)' 
     
    465470    def find_ordering_name(self, name, opts, alias=None, default_order='ASC'): 
    466471        """ 
    467         Returns the table alias (the name might not be unambiguous, the alias 
    468         will be) and column name for ordering by the given 'name' parameter. 
     472        Returns the table alias (the name might be ambiguous, the alias will 
     473        not be) and column name for ordering by the given 'name' parameter. 
    469474        The 'name' is of the form 'field1__field2__...__fieldN'. 
    470475        """ 
  • django/branches/queryset-refactor/tests/regressiontests/queries/models.py

    r6510 r6511  
    290290[<Author: a2>, <Author: a1>, <Author: a4>, <Author: a3>] 
    291291 
     292# Using remote model default ordering can span multiple models (in this case, 
     293# Cover is ordered by Item's default, which uses Note's default). 
     294>>> Cover.objects.all() 
     295[<Cover: first>, <Cover: second>] 
     296 
    292297# If the remote model does not have a default ordering, we order by its 'id' 
    293298# field. 
     
    301306[<Ranking: 1: a3>, <Ranking: 2: a2>, <Ranking: 3: a1>] 
    302307 
    303 >>> Cover.objects.all() 
    304 [<Cover: first>, <Cover: second>] 
     308# Ordering of extra() pieces is possible, too and you can mix extra fields and 
     309# model fields in the ordering. 
     310>>> Ranking.objects.extra(tables=['django_site'], order_by=['-django_site.id', 'rank']) 
     311[<Ranking: 1: a3>, <Ranking: 2: a2>, <Ranking: 3: a1>] 
    305312 
    306313Bugs #2874, #3002