Ticket #15361: 15361-2.2.diff
File 15361-2.2.diff, 4.6 KB (added by , 14 years ago) |
---|
-
docs/ref/models/fields.txt
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index 66bd047..1052782 100644
a b characters that aren't allowed in Python variable names -- notably, the 162 162 hyphen -- that's OK. Django quotes column and table names behind the 163 163 scenes. 164 164 165 .. _field-db_index: 166 165 167 ``db_index`` 166 168 ------------ 167 169 … … override the default primary-key behavior. For more, see 240 242 ``primary_key=True`` implies :attr:`null=False <Field.null>` and :attr:`unique=True <Field.unique>`. 241 243 Only one primary key is allowed on an object. 242 244 245 .. _field-unique: 246 243 247 ``unique`` 244 248 ---------- 245 249 -
docs/topics/db/optimization.txt
diff --git a/docs/topics/db/optimization.txt b/docs/topics/db/optimization.txt index 4c5a389..7d74710 100644
a b Write your own :doc:`custom SQL to retrieve data or populate models 132 132 </topics/db/sql>`. Use ``django.db.connection.queries`` to find out what Django 133 133 is writing for you and start from there. 134 134 135 Retrieve individual objects using a unique, indexed column 136 ========================================================== 137 138 There are two reasons you want to use :ref:`unique <field-unique>`, 139 :ref:`indexed <field-db_index>`: columns to get individual objects. Firstly, 140 the query will be quicker because of the underlying database index. Also 141 the query can run much slower if multiple objects match the lookup; having a 142 unique constraint on the column guarantees this will never happen. 143 144 So using the :ref:`example Weblog models <queryset-model-example>`:: 145 146 >>> entry = Entry.objects.get(id=10) 147 148 will be quicker than the alternative: 149 150 >>> entry = Entry.object.get(headline="News Item Title") 151 152 because ``id`` is indexed by the database and is guaranteed to be unique. 153 154 Doing the following is potentially quite slow. 155 156 >>> entry = Entry.objects.get(headline__startswith="News") 157 158 First of all, `headline` is not indexed, which will make the underlying 159 database fetch slower. 160 161 Secondly, the lookup doesn't guarantee that only one object will be returned. 162 If the query matches more than one object, it will incur the penality for 163 retreiving and transferring all of them from the database. This penality could 164 be substantial if hundreds or thousands of records are returned. The penality 165 will be compounded if the database lives on a separate server, where network 166 overhead and latency also play a factor. 167 168 135 169 Retrieve everything at once if you know you will need it 136 170 ======================================================== 137 171 -
tests/modeltests/basic/tests.py
diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py index 966798d..acb5996 100644
a b 1 1 from datetime import datetime 2 2 3 from django.core.exceptions import ObjectDoesNotExist 3 from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned 4 4 from django.db import models, DEFAULT_DB_ALIAS, connection 5 5 from django.db.models.fields import FieldDoesNotExist 6 6 from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature … … class ModelTest(TestCase): 115 115 b = Article.objects.get(pk=a.id) 116 116 self.assertEqual(a, b) 117 117 118 # Create a very similar object 119 a = Article( 120 id=None, 121 headline='Area man programs in Python', 122 pub_date=datetime(2005, 7, 28), 123 ) 124 a.save() 125 126 self.assertEqual(Article.objects.count(), 2) 127 128 # Django raises an Article.MultipleObjectsReturned exception if the 129 # lookup matches more than one object 130 self.assertRaisesRegexp( 131 MultipleObjectsReturned, 132 "get\(\) returned more than one Article -- it returned 2! Lookup parameters were {'headline__startswith': 'Area'}", 133 Article.objects.get, 134 headline__startswith='Area', 135 ) 136 137 self.assertRaisesRegexp( 138 MultipleObjectsReturned, 139 "get\(\) returned more than one Article -- it returned 2! Lookup parameters were {'pub_date__year': 2005}", 140 Article.objects.get, 141 pub_date__year=2005, 142 ) 143 144 self.assertRaisesRegexp( 145 MultipleObjectsReturned, 146 "get\(\) returned more than one Article -- it returned 2! Lookup parameters were {'pub_date__month': 7, 'pub_date__year': 2005}", 147 Article.objects.get, 148 pub_date__year=2005, 149 pub_date__month=7, 150 ) 151 118 152 def test_object_creation(self): 119 153 # Create an Article. 120 154 a = Article(