Django

Code

Changeset 7492

Show
Ignore:
Timestamp:
04/27/08 23:28:59 (2 months ago)
Author:
brosner
Message:

newforms-admin: Merged from trunk up to [7491].

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin

    • Property svnmerge-integrated changed from /django/trunk:1-4345,4350-4357,4359-4365,4371-4372,4374-4377,4380-4386,4388,4390-4391,4400-4402,4404-4408,4410,4412-4419,4426-4427,4430-4432,4434,4441,4443-4444,4446-4447,4450,4452-4453,4455-4458,4476,4503,4546,4564-4569,4580-4586,4617,4630,4641-6390,6392-7477 to /django/trunk:1-4345,4350-4357,4359-4365,4371-4372,4374-4377,4380-4386,4388,4390-4391,4400-4402,4404-4408,4410,4412-4419,4426-4427,4430-4432,4434,4441,4443-4444,4446-4447,4450,4452-4453,4455-4458,4476,4503,4546,4564-4569,4580-4586,4617,4630,4641-6390,6392-7491
  • django/branches/newforms-admin/django/contrib/admin/views/main.py

    r7479 r7492  
    283283                        break 
    284284 
    285         # Calculate lookup_order_field. 
    286         # If the order-by field is a field with a relationship, order by the 
    287         # value in the related table. 
    288         lookup_order_field = self.order_field 
    289         order_type = self.order_type == 'desc' and '-' or '' 
    290         try: 
    291             f = self.lookup_opts.get_field(self.order_field, many_to_many=False) 
    292         except models.FieldDoesNotExist: 
    293             pass 
    294         else: 
    295             if isinstance(f.rel, models.OneToOneRel): 
    296                 # For OneToOneFields, don't try to order by the related object's ordering criteria. 
    297                 pass 
    298             elif isinstance(f.rel, models.ManyToOneRel): 
    299                 rel_ordering = f.rel.to._meta.ordering and f.rel.to._meta.ordering[0] or f.rel.to._meta.pk.column 
    300                 if rel_ordering[0] == '-': 
    301                     rel_ordering = rel_ordering[1:] 
    302                     order_type = not order_type and '-' or '' 
    303                 lookup_order_field = '%s.%s' % (f.rel.to._meta.db_table, rel_ordering) 
    304                 # FIXME: Must use select_related() becuase the lookup field may 
    305                 # be in a table not otherwise referenced yet. 
    306                 qs = qs.select_related() 
    307  
    308285        # Set ordering. 
    309         qs = qs.order_by(order_type + lookup_order_field) 
     286        if self.order_field: 
     287            qs = qs.order_by('%s%s' % ((self.order_type == 'desc' and '-' or ''), self.order_field)) 
    310288 
    311289        # Apply keyword searches. 
  • django/branches/newforms-admin/django/db/models/query.py

    r7479 r7492  
    285285        query.add_update_values(kwargs) 
    286286        query.execute_sql(None) 
     287        transaction.commit_unless_managed() 
    287288        self._result_cache = None 
    288289    update.alters_data = True 
  • django/branches/newforms-admin/django/db/models/sql/constants.py

    r7479 r7492  
    2929SINGLE = 'single' 
    3030 
    31 ORDER_PATTERN = re.compile(r'\?|[-+]?\w+$') 
     31ORDER_PATTERN = re.compile(r'\?|[-+]?[.\w]+$') 
    3232ORDER_DIR = { 
    3333    'ASC': ('ASC', 'DESC'), 
  • django/branches/newforms-admin/django/db/models/sql/subqueries.py

    r7479 r7492  
    9696    def _setup_query(self): 
    9797        """ 
    98         Runs on initialisation and after cloning. Any attributes that would 
     98        Runs on initialization and after cloning. Any attributes that would 
    9999        normally be set in __init__ should go in here, instead, so that they 
    100100        are also set up after a clone() call. 
     
    350350        self.select = [select] 
    351351        self.select_fields = [None] 
     352        self.select_related = False # See #7097. 
    352353        self.distinct = True 
    353354        self.order_by = order == 'ASC' and [1] or [-1] 
  • django/branches/newforms-admin/docs/db-api.txt

    r7479 r7492  
    393393 
    394394You can also slice from the item ''N'' to the end of the queryset. For 
    395 example, to return everything from the fixth item onwards:: 
     395example, to return everything from the sixth item onwards:: 
    396396 
    397397    Entry.objects.all()[5:] 
     
    528528 
    529529**New in Django development version:** The syntax for ordering across related 
    530 models has changed. See the `Django 0.96 documentation`_ for the old behaviour. 
     530models has changed. See the `Django 0.96 documentation`_ for the old behavior. 
    531531 
    532532.. _Django 0.96 documentation: http://www.djangoproject.com/documentation/0.96/model-api/#floatfield 
     
    541541**New in Django development version** 
    542542 
    543 If you want to reverse the order in which a queryset's elements are returned, 
    544 you can use the ``reverse()`` method. Calling ``reverse()`` a second tim
    545 restores the ordering back to the normal direction. 
     543Use the ``reverse()`` method to reverse the order in which a queryset's 
     544elements are returned. Calling ``reverse()`` a second time restores th
     545ordering back to the normal direction. 
    546546 
    547547To retrieve the ''last'' five items in a queryset, you could do this:: 
     
    553553penultimate item and so on. If we had a Python sequence and looked at 
    554554``seq[:-5]``, we would see the fifth-last item first. Django doesn't support 
    555 that mode of access (slicing from the end), since it is not possible to do it 
     555that mode of access (slicing from the end), because it's not possible to do it 
    556556efficiently in SQL. 
    557557 
     
    571571    Any fields used in an ``order_by()`` call are included in the SQL 
    572572    ``SELECT`` columns. This can sometimes lead to unexpected results when 
    573     used in conjuntion with ``distinct()``. If you order by fields from a 
     573    used in conjunction with ``distinct()``. If you order by fields from a 
    574574    related model, those fields will be added to the selected columns and they 
    575575    may make otherwise duplicate rows appear to be distinct. Since the extra 
     
    684684item is the first field, etc. For example:: 
    685685 
    686     >>> Entry.objects.values_list('id', 'headling') 
     686    >>> Entry.objects.values_list('id', 'headline') 
    687687    [(1, u'First entry'), ...] 
    688688 
     
    838838this for models that are more than one relation away by separating the field 
    839839names with double underscores, just as for filters. For example, if we have 
    840 thise model:: 
     840this model:: 
    841841 
    842842    class Room(models.Model): 
     
    16611661filter statement, not the ``Entry`` items. 
    16621662 
    1663 All of this behaviour also applies to ``exclude()``: all the conditions in a 
     1663All of this behavior also applies to ``exclude()``: all the conditions in a 
    16641664single ``exclude()`` statement apply to a single instance (if those conditions 
    16651665are talking about the same multi-valued relation). Conditions in subsequent 
     
    21022102 
    21032103Sometimes you want to set a field to a particular value for all the objects in 
    2104 a queryset. You can do this with the ``update()`` method. For example:: 
    2105  
    2106     # Update all the headlings to the same value
    2107     Entry.objects.all().update(headline='Everything is the same') 
     2104a ``QuerySet``. You can do this with the ``update()`` method. For example:: 
     2105 
     2106    # Update all the headlines with pub_date in 2007
     2107    Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same') 
    21082108 
    21092109You can only set non-relation fields and ``ForeignKey`` fields using this 
    2110 method and the value you set the field to must be a normal Python value (you 
    2111 can't set a field to be equal to some other field at the moment). 
     2110method, and the value you set the field to must be a hard-coded Python value 
     2111(i.e., you can't set a field to be equal to some other field at the moment). 
    21122112 
    21132113To update ``ForeignKey`` fields, set the new value to be the new model 
     
    21152115 
    21162116    b = Blog.objects.get(pk=1) 
    2117     # Make all entries belong to this blog. 
     2117    # Change every Entry so that it belongs to this Blog. 
    21182118    Entry.objects.all().update(blog=b) 
    21192119 
    21202120The ``update()`` method is applied instantly and doesn't return anything 
    2121 (similar to ``delete()``). The only restriction on the queryset that is 
     2121(similar to ``delete()``). The only restriction on the ``QuerySet`` that is 
    21222122updated is that it can only access one database table, the model's main 
    21232123table. So don't try to filter based on related fields or anything like that; 
  • django/branches/newforms-admin/docs/model-api.txt

    r7479 r7492  
    5353      some model metadata but can be overridden. See `Table names`_ below. 
    5454    * An ``id`` field is added automatically, but this behavior can be 
    55       overriden. See `Automatic primary key fields`_ below. 
     55      overridden. See `Automatic primary key fields`_ below. 
    5656    * The ``CREATE TABLE`` SQL in this example is formatted using PostgreSQL 
    5757      syntax, but it's worth noting Django uses SQL tailored to the database 
     
    21952195The second type of model inheritance supported by Django is when each model in 
    21962196the hierarchy is a model all by itself. Each model corresponds to its own 
    2197 database table and can be queried and created indvidually. The inheritance 
     2197database table and can be queried and created individually. The inheritance 
    21982198relationship introduces links between the child model and each of its parents 
    21992199(via an automatically created ``OneToOneField``). For example:: 
     
    22432243 
    22442244If the parent has an ordering and you don't want the child to have any natural 
    2245 ordering, you can explicity set it to be empty:: 
     2245ordering, you can explicitly set it to be empty:: 
    22462246 
    22472247    class ChildModel(ParentModel): 
  • django/branches/newforms-admin/tests/modeltests/many_to_one/models.py

    r7479 r7492  
    247247[<Reporter: John Smith>] 
    248248 
    249 # Check that implied __exact also works 
     249# Check that implied __exact also works. 
    250250>>> Reporter.objects.filter(article__reporter=r).distinct() 
    251251[<Reporter: John Smith>] 
     
    267267[<Reporter: John Smith>] 
    268268 
    269 # Deletes using a join in the query 
     269# You can delete using a JOIN in the query. 
    270270>>> Reporter.objects.filter(article__headline__startswith='This').delete() 
    271271>>> Reporter.objects.all() 
     
    274274[] 
    275275 
     276# Check that Article.objects.select_related().dates() works properly when 
     277# there are multiple Articles with the same date but different foreign-key 
     278# objects (Reporters). 
     279>>> r1 = Reporter.objects.create(first_name='Mike', last_name='Royko', email='royko@suntimes.com') 
     280>>> r2 = Reporter.objects.create(first_name='John', last_name='Kass', email='jkass@tribune.com') 
     281>>> a1 = Article.objects.create(headline='First', pub_date=datetime(1980, 4, 23), reporter=r1) 
     282>>> a2 = Article.objects.create(headline='Second', pub_date=datetime(1980, 4, 23), reporter=r2) 
     283>>> Article.objects.select_related().dates('pub_date', 'day') 
     284[datetime.datetime(1980, 4, 23, 0, 0)] 
     285>>> Article.objects.select_related().dates('pub_date', 'month') 
     286[datetime.datetime(1980, 4, 1, 0, 0)] 
     287>>> Article.objects.select_related().dates('pub_date', 'year') 
     288[datetime.datetime(1980, 1, 1, 0, 0)] 
    276289"""} 
  • django/branches/newforms-admin/tests/regressiontests/queries/models.py

    r7479 r7492  
    655655>>> s = qs.query.as_sql()   # test passes if this doesn't raise an exception. 
    656656 
     657Bug #7098 -- Make sure semi-deprecated ordering by related models syntax still 
     658works. 
     659>>> Item.objects.values('note__note').order_by('queries_note.note', 'id') 
     660[{'note__note': u'n2'}, {'note__note': u'n3'}, {'note__note': u'n3'}, {'note__note': u'n3'}] 
    657661"""} 
    658662