2005-07-29 17:15:40 +02:00
|
|
|
"""
|
|
|
|
6. Specifying ordering
|
|
|
|
|
|
|
|
Specify default ordering for a model using the ``ordering`` attribute, which
|
2008-02-15 12:38:53 +01:00
|
|
|
should be a list or tuple of field names. This tells Django how to order
|
2008-08-12 16:15:38 +02:00
|
|
|
``QuerySet`` results.
|
2005-07-29 17:15:40 +02:00
|
|
|
|
|
|
|
If a field name in ``ordering`` starts with a hyphen, that field will be
|
|
|
|
ordered in descending order. Otherwise, it'll be ordered in ascending order.
|
|
|
|
The special-case field name ``"?"`` specifies random order.
|
|
|
|
|
|
|
|
The ordering attribute is not required. If you leave it off, ordering will be
|
|
|
|
undefined -- not random, just undefined.
|
|
|
|
"""
|
|
|
|
|
2006-05-02 03:31:56 +02:00
|
|
|
from django.db import models
|
2012-08-12 12:32:08 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2005-07-29 17:15:40 +02:00
|
|
|
|
2010-10-11 20:17:37 +02:00
|
|
|
|
2014-04-26 09:34:20 +02:00
|
|
|
class Author(models.Model):
|
2006-05-02 03:31:56 +02:00
|
|
|
class Meta:
|
2014-04-26 09:34:20 +02:00
|
|
|
ordering = ('-pk',)
|
2012-02-04 20:56:40 +01:00
|
|
|
|
2013-11-02 22:34:05 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
@python_2_unicode_compatible
|
2014-04-26 09:34:20 +02:00
|
|
|
class Article(models.Model):
|
|
|
|
author = models.ForeignKey(Author, null=True)
|
2012-02-04 20:56:40 +01:00
|
|
|
headline = models.CharField(max_length=100)
|
|
|
|
pub_date = models.DateTimeField()
|
2013-10-22 12:21:07 +02:00
|
|
|
|
2012-02-04 20:56:40 +01:00
|
|
|
class Meta:
|
2014-04-26 09:34:20 +02:00
|
|
|
ordering = ('-pub_date', 'headline')
|
2012-02-04 20:56:40 +01:00
|
|
|
|
2012-08-12 12:32:08 +02:00
|
|
|
def __str__(self):
|
2012-02-04 20:56:40 +01:00
|
|
|
return self.headline
|