Changeset 4400
- Timestamp:
- 01/22/07 22:31:02 (2 years ago)
- Files:
-
- django/branches/newforms-admin/django/db/models/manager.py (modified) (3 diffs)
- django/branches/newforms-admin/django/db/models/query.py (modified) (9 diffs)
- django/branches/newforms-admin/django/oldforms/__init__.py (modified) (1 diff)
- django/branches/newforms-admin/docs/db-api.txt (modified) (1 diff)
- django/branches/newforms-admin/docs/generic_views.txt (modified) (11 diffs)
- django/branches/newforms-admin/docs/model-api.txt (modified) (1 diff)
- django/branches/newforms-admin/docs/settings.txt (modified) (1 diff)
- django/branches/newforms-admin/docs/tutorial03.txt (modified) (1 diff)
- django/branches/newforms-admin/docs/url_dispatch.txt (modified) (2 diffs)
- django/branches/newforms-admin/tests/modeltests/lookup/models.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/newforms-admin/django/db/models/manager.py
r4265 r4400 1 from django.db.models.query import QuerySet 1 from django.db.models.query import QuerySet, EmptyQuerySet 2 2 from django.dispatch import dispatcher 3 3 from django.db.models import signals … … 42 42 # PROXIES TO QUERYSET # 43 43 ####################### 44 45 def get_empty_query_set(self): 46 return EmptyQuerySet(self.model) 44 47 45 48 def get_query_set(self): … … 48 51 """ 49 52 return QuerySet(self.model) 53 54 def none(self): 55 return self.get_empty_query_set() 50 56 51 57 def all(self): django/branches/newforms-admin/django/db/models/query.py
r4283 r4400 25 25 # Larger values are slightly faster at the expense of more storage space. 26 26 GET_ITERATOR_CHUNK_SIZE = 100 27 28 class EmptyResultSet(Exception): 29 pass 27 30 28 31 #################### … … 169 172 170 173 cursor = connection.cursor() 171 select, sql, params = self._get_sql_clause() 174 175 try: 176 select, sql, params = self._get_sql_clause() 177 except EmptyResultSet: 178 raise StopIteration 179 172 180 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 173 181 fill_cache = self._select_related … … 193 201 counter._limit = None 194 202 counter._select_related = False 195 select, sql, params = counter._get_sql_clause() 203 204 try: 205 select, sql, params = counter._get_sql_clause() 206 except EmptyResultSet: 207 return 0 208 196 209 cursor = connection.cursor() 197 210 if self._distinct: … … 524 537 525 538 cursor = connection.cursor() 526 select, sql, params = self._get_sql_clause() 539 540 try: 541 select, sql, params = self._get_sql_clause() 542 except EmptyResultSet: 543 raise StopIteration 544 527 545 select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] 528 546 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) … … 546 564 self._where.append('%s.%s IS NOT NULL' % \ 547 565 (backend.quote_name(self.model._meta.db_table), backend.quote_name(self._field.column))) 548 select, sql, params = self._get_sql_clause() 566 567 try: 568 select, sql, params = self._get_sql_clause() 569 except EmptyResultSet: 570 raise StopIteration 571 549 572 sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1 %s' % \ 550 573 (backend.get_date_trunc_sql(self._kind, '%s.%s' % (backend.quote_name(self.model._meta.db_table), … … 563 586 c._order = self._order 564 587 return c 588 589 class EmptyQuerySet(QuerySet): 590 def __init__(self, model=None): 591 super(EmptyQuerySet, self).__init__(model) 592 self._result_cache = [] 593 594 def iterator(self): 595 raise StopIteration 596 597 def count(self): 598 return 0 599 600 def delete(self): 601 pass 602 603 def _clone(self, klass=None, **kwargs): 604 c = super(EmptyQuerySet, self)._clone(klass, **kwargs) 605 c._result_cache = [] 606 return c 565 607 566 608 class QOperator(object): … … 572 614 joins, where, params = SortedDict(), [], [] 573 615 for val in self.args: 574 joins2, where2, params2 = val.get_sql(opts) 575 joins.update(joins2) 576 where.extend(where2) 577 params.extend(params2) 616 try: 617 joins2, where2, params2 = val.get_sql(opts) 618 joins.update(joins2) 619 where.extend(where2) 620 params.extend(params2) 621 except EmptyResultSet: 622 if not isinstance(self, QOr): 623 raise EmptyResultSet 578 624 if where: 579 625 return joins, ['(%s)' % self.operator.join(where)], params … … 629 675 630 676 def get_sql(self, opts): 631 joins, where, params = self.q.get_sql(opts) 632 where2 = ['(NOT (%s))' % " AND ".join(where)] 677 try: 678 joins, where, params = self.q.get_sql(opts) 679 where2 = ['(NOT (%s))' % " AND ".join(where)] 680 except EmptyResultSet: 681 return SortedDict(), [], [] 633 682 return joins, where2, params 634 683 … … 646 695 return '%s%s IN (%s)' % (table_prefix, field_name, in_string) 647 696 else: 648 # Most backends do not accept an empty string inside the IN 649 # expression, i.e. cannot do "WHERE ... IN ()". Since there are 650 # also some backends that do not accept "WHERE false", we instead 651 # use an expression that always evaluates to False. 652 return '0=1' 697 raise EmptyResultSet 653 698 elif lookup_type == 'range': 654 699 return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name) django/branches/newforms-admin/django/oldforms/__init__.py
r4029 r4400 570 570 def __init__(self, field_name, is_required=False, validator_list=None): 571 571 if validator_list is None: validator_list = [] 572 SelectField.__init__(self, field_name, choices=[('1', 'Unknown'), ('2', 'Yes'), ('3', 'No')],572 SelectField.__init__(self, field_name, choices=[('1', _('Unknown')), ('2', _('Yes')), ('3', _('No'))], 573 573 is_required=is_required, validator_list=validator_list) 574 574 django/branches/newforms-admin/docs/db-api.txt
r4281 r4400 526 526 >>> Entry.objects.filter(headline__contains='Lennon').dates('pub_date', 'day') 527 527 [datetime.datetime(2005, 3, 20)] 528 529 ``none()`` 530 ~~~~~~~~~~ 531 532 **New in Django development version** 533 534 Returns an ``EmptyQuerySet`` -- a ``QuerySet`` that always evaluates to 535 an empty list. This can be used in cases where you know that you should 536 return an empty result set and your caller is expecting a ``QuerySet`` 537 object (instead of returning an empty list, for example.) 538 539 Examples:: 540 541 >>> Entry.objects.none() 542 [] 528 543 529 544 ``select_related()`` django/branches/newforms-admin/docs/generic_views.txt
r4208 r4400 100 100 just before rendering the template. (**This is new in the 101 101 Django development version.**) 102 102 103 103 **Example:** 104 104 … … 206 206 207 207 * ``<model_name>`` is your model's name in all lowercase. For a model 208 ``StaffMember``, that'd be ``staffmember``.208 ``StaffMember``, that'd be ``staffmember``. 209 209 210 210 * ``<app_label>`` is the right-most part of the full Python path to 211 your model's app. For example, if your model lives in212 ``apps/blog/models.py``, that'd be ``blog``.211 your model's app. For example, if your model lives in 212 ``apps/blog/models.py``, that'd be ``blog``. 213 213 214 214 **Template context:** … … 267 267 268 268 * ``template_object_name``: Designates the name of the template variable 269 to use in the template context. By default, this is ``'object'``. The270 view will append ``'_list'`` to the value of this parameter in271 determining the variable's name.269 to use in the template context. By default, this is ``'object'``. The 270 view will append ``'_list'`` to the value of this parameter in 271 determining the variable's name. 272 272 273 273 * ``make_object_list``: A boolean specifying whether to retrieve the full … … 361 361 362 362 * ``template_object_name``: Designates the name of the template variable 363 to use in the template context. By default, this is ``'object'``. The364 view will append ``'_list'`` to the value of this parameter in365 determining the variable's name.363 to use in the template context. By default, this is ``'object'``. The 364 view will append ``'_list'`` to the value of this parameter in 365 determining the variable's name. 366 366 367 367 * ``mimetype``: The MIME type to use for the resulting document. Defaults … … 442 442 443 443 * ``template_object_name``: Designates the name of the template variable 444 to use in the template context. By default, this is ``'object'``. The445 view will append ``'_list'`` to the value of this parameter in446 determining the variable's name.444 to use in the template context. By default, this is ``'object'``. The 445 view will append ``'_list'`` to the value of this parameter in 446 determining the variable's name. 447 447 448 448 * ``mimetype``: The MIME type to use for the resulting document. Defaults … … 527 527 528 528 * ``template_object_name``: Designates the name of the template variable 529 to use in the template context. By default, this is ``'object'``. The530 view will append ``'_list'`` to the value of this parameter in531 determining the variable's name.529 to use in the template context. By default, this is ``'object'``. The 530 view will append ``'_list'`` to the value of this parameter in 531 determining the variable's name. 532 532 533 533 * ``mimetype``: The MIME type to use for the resulting document. Defaults … … 639 639 640 640 * ``template_object_name``: Designates the name of the template variable 641 to use in the template context. By default, this is ``'object'``.641 to use in the template context. By default, this is ``'object'``. 642 642 643 643 * ``mimetype``: The MIME type to use for the resulting document. Defaults … … 711 711 712 712 * ``template_object_name``: Designates the name of the template variable 713 to use in the template context. By default, this is ``'object'``. The714 view will append ``'_list'`` to the value of this parameter in715 determining the variable's name.713 to use in the template context. By default, this is ``'object'``. The 714 view will append ``'_list'`` to the value of this parameter in 715 determining the variable's name. 716 716 717 717 * ``mimetype``: The MIME type to use for the resulting document. Defaults … … 825 825 826 826 * ``template_object_name``: Designates the name of the template variable 827 to use in the template context. By default, this is ``'object'``.827 to use in the template context. By default, this is ``'object'``. 828 828 829 829 * ``mimetype``: The MIME type to use for the resulting document. Defaults … … 974 974 975 975 * ``template_object_name``: Designates the name of the template variable 976 to use in the template context. By default, this is ``'object'``.976 to use in the template context. By default, this is ``'object'``. 977 977 978 978 **Template name:** … … 1055 1055 1056 1056 * ``template_object_name``: Designates the name of the template variable 1057 to use in the template context. By default, this is ``'object'``.1057 to use in the template context. By default, this is ``'object'``. 1058 1058 1059 1059 **Template name:** django/branches/newforms-admin/docs/model-api.txt
r4309 r4400 1409 1409 1410 1410 These fields should be some kind of text field, such as ``CharField`` or 1411 ``TextField``. 1411 ``TextField``. You can also perform a related lookup on a ``ForeignKey`` with 1412 the lookup API "follow" notation:: 1413 1414 search_fields = ['foreign_key__related_fieldname'] 1412 1415 1413 1416 When somebody does a search in the admin search box, Django splits the search django/branches/newforms-admin/docs/settings.txt
r4280 r4400 558 558 Example: ``"http://media.lawrence.com"`` 559 559 560 Note that this should have a trailing slash if it has a path component. 561 562 Good: ``"http://www.example.com/static/"`` 563 Bad: ``"http://www.example.com/static"`` 564 560 565 MIDDLEWARE_CLASSES 561 566 ------------------ django/branches/newforms-admin/docs/tutorial03.txt
r4275 r4400 261 261 return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list}) 262 262 263 Note that we no longer need to import ``loader``, ``Context`` or 264 ``HttpResponse``. 263 Note that once we've done this in all these views, we no longer need to import ``loader``, ``Context`` and ``HttpResponse``. 265 264 266 265 The ``render_to_response()`` function takes a template name as its first django/branches/newforms-admin/docs/url_dispatch.txt
r4371 r4400 394 394 -------------------------------------- 395 395 396 **New in theDjango development version.**396 **New in Django development version.** 397 397 398 398 Similarly, you can pass extra options to ``include()``. When you pass extra … … 436 436 =========================================== 437 437 438 **New in theDjango development version.**438 **New in Django development version.** 439 439 440 440 Some developers find it more natural to pass the actual Python function object django/branches/newforms-admin/tests/modeltests/lookup/models.py
r3661 r4400 192 192 [<Article: Article with \ backslash>] 193 193 194 # none() returns an EmptyQuerySet that behaves like any other QuerySet object 195 >>> Article.objects.none() 196 [] 197 >>> Article.objects.none().filter(headline__startswith='Article') 198 [] 199 >>> Article.objects.none().count() 200 0 201 202 # using __in with an empty list should return an empty query set 203 >>> Article.objects.filter(id__in=[]) 204 [] 205 206 >>> Article.objects.exclude(id__in=[]) 207 [<Article: Article with \ backslash>, <Article: Article% with percent sign>, <Article: Article_ with underscore>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 7>, <Article: Article 1>] 208 194 209 """}
