1
0
mirror of https://github.com/django/django.git synced 2025-08-06 18:09:12 +00:00

Fixed #153 -- Changed docs to use new ordering syntax

git-svn-id: http://code.djangoproject.com/svn/django/trunk@299 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-07-22 18:45:22 +00:00
parent c97efb6799
commit c2a5c49ac2
3 changed files with 21 additions and 22 deletions

View File

@ -107,14 +107,15 @@ provided by the ``order_by`` argument to a lookup::
polls.get_list( polls.get_list(
pub_date__year=2005, pub_date__year=2005,
pub_date__month=1, pub_date__month=1,
order_by=(("pub_date", "DESC"), ("question", "ASC")), order_by=('-pub_date', 'question'),
) )
The result set above will be ordered by ``pub_date`` (descending), then The result set above will be ordered by ``pub_date`` descending, then
by ``question`` (ascending). Just like in models, the ``order_by`` clause by ``question`` ascending. The negative sign in front of "-pub_date" indicates
is a list of ordering tuples where the first element is the field and the descending order. Ascending order is implied. To order randomly, use "?", like
second is "ASC" (ascending) or "DESC" (descending). You can also so::
use the tuple ``(None, "RANDOM")`` to order the result set randomly.
polls.get_list(order_by=['?'])
Relationships (joins) Relationships (joins)
===================== =====================

View File

@ -76,11 +76,11 @@ wide array of options, only ``fields`` is required.
``ordering`` ``ordering``
The default ordering for the object, for use by ``get_list`` and the admin:: The default ordering for the object, for use by ``get_list`` and the admin::
ordering = (('order_date', 'DESC'),) ordering = ['-order_date']
This is a tuple of 2-tuples. Each 2-tuple is ``(field_name, ordering_type)`` This is a tuple or list of strings. Each string is a field name with an
where ordering_type is either ``"ASC"`` or ``"DESC"``. You can also use the optional "-" (indicating descending order). Or, you can use the string "?"
``(None, "RANDOM")`` for random ordering. to order randomly.
``permissions`` ``permissions``
Extra permissions to enter into the permissions table when creating this Extra permissions to enter into the permissions table when creating this
@ -662,7 +662,7 @@ object, which has the following options. All are optional.
(This example also has ``search_fields`` defined; see below). (This example also has ``search_fields`` defined; see below).
``ordering`` ``ordering``
An ordering tuple (see the `Options for models`_, above) that gives a A list or tuple (see the `Options for models`_, above) that gives a
different ordering for the admin change list. If this isn't given, the different ordering for the admin change list. If this isn't given, the
model's default ordering will be used. model's default ordering will be used.

View File

@ -14,25 +14,25 @@ application and will focus on creating the public interface -- "views."
A view is a "type" of Web page in your Django application that generally A view is a "type" of Web page in your Django application that generally
serves a specific function and has a specific template. For example, in a serves a specific function and has a specific template. For example, in a
weblog application, you might have the following views: weblog application, you might have the following views:
* Blog homepage -- displays the latest few entries. * Blog homepage -- displays the latest few entries.
* Entry "detail" page -- permalink page for a single entry. * Entry "detail" page -- permalink page for a single entry.
* Year-based archive page -- displays all months with entries in the * Year-based archive page -- displays all months with entries in the
given year. given year.
* Month-based archive page -- displays all days with entries in the * Month-based archive page -- displays all days with entries in the
given month. given month.
* Day-based archive page -- displays all entries in the given day. * Day-based archive page -- displays all entries in the given day.
* Comment action -- handles posting comments to a given entry. * Comment action -- handles posting comments to a given entry.
In our poll application, we'll have the following four views: In our poll application, we'll have the following four views:
* Poll "archive" page -- displays the latest few polls. * Poll "archive" page -- displays the latest few polls.
* Poll "detail" page -- displays a poll question, with no results but * Poll "detail" page -- displays a poll question, with no results but
with a form to vote. with a form to vote.
* Poll "results" page -- displays results for a particular poll. * Poll "results" page -- displays results for a particular poll.
* Vote action -- handles voting for a particular choice in a particular * Vote action -- handles voting for a particular choice in a particular
poll. poll.
In Django, each view is represented by a simple Python function. In Django, each view is represented by a simple Python function.
Design your URLs Design your URLs
@ -174,8 +174,7 @@ publication date::
from django.utils.httpwrappers import HttpResponse from django.utils.httpwrappers import HttpResponse
def index(request): def index(request):
latest_poll_list = polls.get_list(order_by=[('pub_date', 'DESC')], latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)
limit=5)
output = ', '.join([p.question for p in latest_poll_list]) output = ', '.join([p.question for p in latest_poll_list])
return HttpResponse(output) return HttpResponse(output)
@ -189,8 +188,7 @@ So let's use Django's template system to separate the design from Python::
from django.utils.httpwrappers import HttpResponse from django.utils.httpwrappers import HttpResponse
def index(request): def index(request):
latest_poll_list = polls.get_list(order_by=[('pub_date', 'DESC')], latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)
limit=5)
t = template_loader.get_template('polls/index') t = template_loader.get_template('polls/index')
c = Context(request, { c = Context(request, {
'latest_poll_list': latest_poll_list, 'latest_poll_list': latest_poll_list,
@ -278,7 +276,7 @@ Two more things to note about 404 views:
* The 404 view is also called if Django doesn't find a match after checking * The 404 view is also called if Django doesn't find a match after checking
every regular expression in the URLconf. every regular expression in the URLconf.
* If you don't define your own 404 view -- and simply use the default, * If you don't define your own 404 view -- and simply use the default,
which is recommended -- you still have one obligation: To create a which is recommended -- you still have one obligation: To create a
``404.html`` template in the root of your template directory. The default ``404.html`` template in the root of your template directory. The default
404 view will use that template for all 404 errors. 404 view will use that template for all 404 errors.