Changeset 7492
- Timestamp:
- 04/27/08 23:28:59 (2 months ago)
- Files:
-
- django/branches/newforms-admin (modified) (1 prop)
- django/branches/newforms-admin/django/contrib/admin/views/main.py (modified) (1 diff)
- django/branches/newforms-admin/django/db/models/query.py (modified) (1 diff)
- django/branches/newforms-admin/django/db/models/sql/constants.py (modified) (1 diff)
- django/branches/newforms-admin/django/db/models/sql/subqueries.py (modified) (2 diffs)
- django/branches/newforms-admin/docs/db-api.txt (modified) (10 diffs)
- django/branches/newforms-admin/docs/model-api.txt (modified) (3 diffs)
- django/branches/newforms-admin/tests/modeltests/many_to_one/models.py (modified) (3 diffs)
- django/branches/newforms-admin/tests/regressiontests/queries/models.py (modified) (1 diff)
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 283 283 break 284 284 285 # Calculate lookup_order_field.286 # If the order-by field is a field with a relationship, order by the287 # value in the related table.288 lookup_order_field = self.order_field289 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 pass294 else:295 if isinstance(f.rel, models.OneToOneRel):296 # For OneToOneFields, don't try to order by the related object's ordering criteria.297 pass298 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.column300 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 may305 # be in a table not otherwise referenced yet.306 qs = qs.select_related()307 308 285 # 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)) 310 288 311 289 # Apply keyword searches. django/branches/newforms-admin/django/db/models/query.py
r7479 r7492 285 285 query.add_update_values(kwargs) 286 286 query.execute_sql(None) 287 transaction.commit_unless_managed() 287 288 self._result_cache = None 288 289 update.alters_data = True django/branches/newforms-admin/django/db/models/sql/constants.py
r7479 r7492 29 29 SINGLE = 'single' 30 30 31 ORDER_PATTERN = re.compile(r'\?|[-+]? \w+$')31 ORDER_PATTERN = re.compile(r'\?|[-+]?[.\w]+$') 32 32 ORDER_DIR = { 33 33 'ASC': ('ASC', 'DESC'), django/branches/newforms-admin/django/db/models/sql/subqueries.py
r7479 r7492 96 96 def _setup_query(self): 97 97 """ 98 Runs on initiali sation and after cloning. Any attributes that would98 Runs on initialization and after cloning. Any attributes that would 99 99 normally be set in __init__ should go in here, instead, so that they 100 100 are also set up after a clone() call. … … 350 350 self.select = [select] 351 351 self.select_fields = [None] 352 self.select_related = False # See #7097. 352 353 self.distinct = True 353 354 self.order_by = order == 'ASC' and [1] or [-1] django/branches/newforms-admin/docs/db-api.txt
r7479 r7492 393 393 394 394 You can also slice from the item ''N'' to the end of the queryset. For 395 example, to return everything from the fixth item onwards::395 example, to return everything from the sixth item onwards:: 396 396 397 397 Entry.objects.all()[5:] … … 528 528 529 529 **New in Django development version:** The syntax for ordering across related 530 models has changed. See the `Django 0.96 documentation`_ for the old behavio ur.530 models has changed. See the `Django 0.96 documentation`_ for the old behavior. 531 531 532 532 .. _Django 0.96 documentation: http://www.djangoproject.com/documentation/0.96/model-api/#floatfield … … 541 541 **New in Django development version** 542 542 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 time545 restores theordering back to the normal direction.543 Use the ``reverse()`` method to reverse the order in which a queryset's 544 elements are returned. Calling ``reverse()`` a second time restores the 545 ordering back to the normal direction. 546 546 547 547 To retrieve the ''last'' five items in a queryset, you could do this:: … … 553 553 penultimate item and so on. If we had a Python sequence and looked at 554 554 ``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 it555 that mode of access (slicing from the end), because it's not possible to do it 556 556 efficiently in SQL. 557 557 … … 571 571 Any fields used in an ``order_by()`` call are included in the SQL 572 572 ``SELECT`` columns. This can sometimes lead to unexpected results when 573 used in conjun tion with ``distinct()``. If you order by fields from a573 used in conjunction with ``distinct()``. If you order by fields from a 574 574 related model, those fields will be added to the selected columns and they 575 575 may make otherwise duplicate rows appear to be distinct. Since the extra … … 684 684 item is the first field, etc. For example:: 685 685 686 >>> Entry.objects.values_list('id', 'headlin g')686 >>> Entry.objects.values_list('id', 'headline') 687 687 [(1, u'First entry'), ...] 688 688 … … 838 838 this for models that are more than one relation away by separating the field 839 839 names with double underscores, just as for filters. For example, if we have 840 this emodel::840 this model:: 841 841 842 842 class Room(models.Model): … … 1661 1661 filter statement, not the ``Entry`` items. 1662 1662 1663 All of this behavio ur also applies to ``exclude()``: all the conditions in a1663 All of this behavior also applies to ``exclude()``: all the conditions in a 1664 1664 single ``exclude()`` statement apply to a single instance (if those conditions 1665 1665 are talking about the same multi-valued relation). Conditions in subsequent … … 2102 2102 2103 2103 Sometimes 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 headlin gs to the same value.2107 Entry.objects. all().update(headline='Everything is the same')2104 a ``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') 2108 2108 2109 2109 You 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 (you2111 can't set a field to be equal to some other field at the moment).2110 method, 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). 2112 2112 2113 2113 To update ``ForeignKey`` fields, set the new value to be the new model … … 2115 2115 2116 2116 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. 2118 2118 Entry.objects.all().update(blog=b) 2119 2119 2120 2120 The ``update()`` method is applied instantly and doesn't return anything 2121 (similar to ``delete()``). The only restriction on the querysetthat is2121 (similar to ``delete()``). The only restriction on the ``QuerySet`` that is 2122 2122 updated is that it can only access one database table, the model's main 2123 2123 table. So don't try to filter based on related fields or anything like that; django/branches/newforms-admin/docs/model-api.txt
r7479 r7492 53 53 some model metadata but can be overridden. See `Table names`_ below. 54 54 * An ``id`` field is added automatically, but this behavior can be 55 overrid en. See `Automatic primary key fields`_ below.55 overridden. See `Automatic primary key fields`_ below. 56 56 * The ``CREATE TABLE`` SQL in this example is formatted using PostgreSQL 57 57 syntax, but it's worth noting Django uses SQL tailored to the database … … 2195 2195 The second type of model inheritance supported by Django is when each model in 2196 2196 the hierarchy is a model all by itself. Each model corresponds to its own 2197 database table and can be queried and created ind vidually. The inheritance2197 database table and can be queried and created individually. The inheritance 2198 2198 relationship introduces links between the child model and each of its parents 2199 2199 (via an automatically created ``OneToOneField``). For example:: … … 2243 2243 2244 2244 If the parent has an ordering and you don't want the child to have any natural 2245 ordering, you can explicit y set it to be empty::2245 ordering, you can explicitly set it to be empty:: 2246 2246 2247 2247 class ChildModel(ParentModel): django/branches/newforms-admin/tests/modeltests/many_to_one/models.py
r7479 r7492 247 247 [<Reporter: John Smith>] 248 248 249 # Check that implied __exact also works 249 # Check that implied __exact also works. 250 250 >>> Reporter.objects.filter(article__reporter=r).distinct() 251 251 [<Reporter: John Smith>] … … 267 267 [<Reporter: John Smith>] 268 268 269 # Deletes using a join in the query269 # You can delete using a JOIN in the query. 270 270 >>> Reporter.objects.filter(article__headline__startswith='This').delete() 271 271 >>> Reporter.objects.all() … … 274 274 [] 275 275 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)] 276 289 """} django/branches/newforms-admin/tests/regressiontests/queries/models.py
r7479 r7492 655 655 >>> s = qs.query.as_sql() # test passes if this doesn't raise an exception. 656 656 657 Bug #7098 -- Make sure semi-deprecated ordering by related models syntax still 658 works. 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'}] 657 661 """} 658 662
