Django

Code

Changeset 7220

Show
Ignore:
Timestamp:
03/11/08 00:21:50 (7 months ago)
Author:
mtredinnick
Message:

queyrset-refactor: Added error reporting if somebody tries to order by a multi-valued field.

Files:

Legend:

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

    r7217 r7220  
    510510        if not alias: 
    511511            alias = self.get_initial_alias() 
    512         field, target, opts, joins = self.setup_joins(pieces, opts, alias, 
    513                 False) 
     512        result = self.setup_joins(pieces, opts, alias, False, False) 
     513        if isinstance(result, int): 
     514            raise FieldError("Cannot order by many-valued field: '%s'" % name) 
     515        field, target, opts, joins = result 
    514516        alias = joins[-1][-1] 
    515517        col = target.column 
  • django/branches/queryset-refactor/docs/db-api.txt

    r7149 r7220  
    541541 
    542542...since the ``Blog`` model has no default ordering specified. 
     543 
     544You can only order by model fields that have a single value attached to them 
     545for each instance of the model. For example, non-relations, ``ForeignKey`` and 
     546``OneToOneField`` fields. Explicitly, you can't order by a ``ManyToManyField`` 
     547or a reverse ``ForeignKey`` relation. There's no naturally correct ordering 
     548for many-valued fields and a lot of the alternatives are not psosible to 
     549express in SQL very efficiently. 
    543550 
    544551**New in Django development version:** If you don't want any ordering to be 
  • django/branches/queryset-refactor/tests/regressiontests/queries/models.py

    r7217 r7220  
    419419[<Ranking: 1: a3>, <Ranking: 2: a2>, <Ranking: 3: a1>] 
    420420 
     421# Ordering by a many-valued attribute (e.g. a many-to-many or reverse 
     422# ForeignKey) doesn't make sense (there's no natural ordering). 
     423>>> Item.objects.all().order_by('tags') 
     424Traceback (most recent call last): 
     425... 
     426FieldError: Cannot order by many-valued field: 'tags' 
     427 
    421428# If we replace the default ordering, Django adjusts the required tables 
    422429# automatically. Item normally requires a join with Note to do the default